NodeIterator

NodeIterator interface represents an iterator over the members of a list of the nodes in a subtree of the DOM. The nodes will be returned in document order.

句法

A NodeIterator can be created using the Document.createNodeIterator() method, as follows:

const nodeIterator = document.createNodeIterator(root, whatToShow, filter);
					

特性

This interface doesn't inherit any property.

NodeIterator.root 只读
返回 Node representing the root node as specified when the NodeIterator was created.
NodeIterator.whatToShow 只读

Returns an unsigned long being a bitmask made of constants describing the types of Node that must to be presented. Non-matching nodes are skipped, but their children may be included, if relevant.

可能的值:

常量 Numerical value 描述
NodeFilter.SHOW_ALL -1 (that is the max value of unsigned long ) Shows all nodes.
NodeFilter.SHOW_ATTRIBUTE 2 Shows attribute Attr nodes. This is meaningful only when creating a NodeIterator with an Attr node as its root; in this case, it means that the attribute node will appear in the first position of the iteration or traversal. Since attributes are never children of other nodes, they do not appear when traversing over the document tree.
NodeFilter.SHOW_CDATA_SECTION 8 Shows CDATASection nodes.
NodeFilter.SHOW_COMMENT 128 Shows Comment nodes.
NodeFilter.SHOW_DOCUMENT 256 Shows Document nodes.
NodeFilter.SHOW_DOCUMENT_FRAGMENT 1024 Shows DocumentFragment nodes.
NodeFilter.SHOW_DOCUMENT_TYPE 512 Shows DocumentType nodes.
NodeFilter.SHOW_ELEMENT 1 Shows Element nodes.
NodeFilter.SHOW_ENTITY 32 Shows Entity nodes. This is meaningful only when creating a NodeIterator with an Entity node as its root; in this case, it means that the Entity node will appear in the first position of the traversal. Since entities are not part of the document tree, they do not appear when traversing over the document tree.
NodeFilter.SHOW_ENTITY_REFERENCE 16 Shows EntityReference nodes.
NodeFilter.SHOW_NOTATION 2048 Shows Notation nodes. This is meaningful only when creating a NodeIterator 采用 Notation node as its root; in this case, it means that the Notation node will appear in the first position of the traversal. Since entities are not part of the document tree, they do not appear when traversing over the document tree.
NodeFilter.SHOW_PROCESSING_INSTRUCTION 64 Shows ProcessingInstruction nodes.
NodeFilter.SHOW_TEXT 4 Shows Text nodes.
NodeIterator.filter 只读
返回 NodeFilter used to select the relevant nodes.
NodeIterator.expandEntityReferences 只读
Boolean indicating if, when discarding an EntityReference its whole sub-tree must be discarded at the same time.
NodeIterator.referenceNode 只读
返回 Node to which the iterator is anchored.
NodeIterator.pointerBeforeReferenceNode 只读
返回 Boolean flag that indicates whether the NodeIterator is anchored before, the flag being true , or after, the flag being false , the anchor node.

方法

This interface doesn't inherit any method.

NodeIterator.detach()
This operation is a no-op. It doesn't do anything. Previously it was telling the engine that the NodeIterator was no more used, but this is now useless.
NodeIterator.previousNode()
Returns the previous Node in the document, or null if there are none.
NodeIterator.nextNode()
返回下一 Node in the document, or null if there are none.

规范

规范 状态 Comment
DOM
The definition of 'NodeIterator' in that specification.
实时标准 添加 referenceNode and pointerBeforeReferenceNode 特性。
Removed the expandEntityReferences 特性。
The method detach() has been changed to be a no-op.
The methods previousNode() and nextNode() don't raise an exception any more.
Document Object Model (DOM) Level 2 Traversal and Range Specification
The definition of 'NodeIterator' in that specification.
Obsolete 初始定义。

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
NodeIterator Chrome 1 Edge 12 Firefox 3.5 IE 9 Opera 9 Safari 3 WebView Android Yes Chrome Android Yes Firefox Android 4 Opera Android 10.1 Safari iOS 3 Samsung Internet Android Yes
detach
弃用 非标
Chrome 1 Edge 12 Firefox 3.5 — 22 IE 9 Opera 9 Safari 3 WebView Android Yes Chrome Android Yes Firefox Android 4 — 22 Opera Android 10.1 Safari iOS 3 Samsung Internet Android Yes
expandEntityReferences
弃用 非标
Chrome 1 Edge 12 Firefox 3.5 — 21 IE 9 Opera 9 Safari 3 WebView Android Yes Chrome Android Yes Firefox Android 4 — 21 Opera Android 10.1 Safari iOS 3 Samsung Internet Android Yes
filter Chrome 1 Edge 12 Firefox 3.5 IE 9 Opera 9 Safari 3 WebView Android Yes Chrome Android Yes Firefox Android 4 Opera Android 10.1 Safari iOS 3 Samsung Internet Android Yes
nextNode Chrome 1 Edge 12 Firefox 3.5 IE 9 Opera 9 Safari 3 WebView Android Yes Chrome Android Yes Firefox Android 4 Opera Android 10.1 Safari iOS 3 Samsung Internet Android Yes
pointerBeforeReferenceNode
Chrome 1 Edge 17 Firefox 3.5 IE No Opera Yes Safari 3 WebView Android Yes Chrome Android Yes Firefox Android 4 Opera Android Yes Safari iOS 3 Samsung Internet Android Yes
previousNode Chrome 1 Edge 12 Firefox 3.5 IE 9 Opera 9 Safari 3 WebView Android Yes Chrome Android Yes Firefox Android 4 Opera Android 10.1 Safari iOS 3 Samsung Internet Android Yes
referenceNode
Chrome 1 Edge 17 Firefox 3.5 IE No Opera Yes Safari 3 WebView Android Yes Chrome Android Yes Firefox Android 4 Opera Android Yes Safari iOS 3 Samsung Internet Android Yes
root Chrome 1 Edge 12 Firefox 3.5 IE 9 Opera 9 Safari 3 WebView Android Yes Chrome Android Yes Firefox Android 4 Opera Android 10.1 Safari iOS 3 Samsung Internet Android Yes
whatToShow Chrome 1 Edge 12 Firefox 3.5 IE 9 Opera 9 Safari 3 WebView Android Yes Chrome Android Yes Firefox Android 4 Opera Android 10.1 Safari iOS 3 Samsung Internet Android Yes

图例

完整支持
完整支持
不支持
不支持
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
非标。预期跨浏览器支持较差。
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
弃用。不要用于新网站。

另请参阅