Document.createNodeIterator()

这篇翻译不完整。请帮忙从英语翻译这篇文章

返回一个新的 NodeIterator 对象.

译注:该函数的作用是用来筛选元素(节点)的。

语法

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

参数

root
 NodeIterator的起点,也就是筛选的范围,筛选root元素内的节点。
whatToShow 可选
是一个可选的无符号长整形(unsigned long)数字去代表筛选的类型,也可是使用 NodeFilter 对象的常量来表示他表示筛选的元素类型。 默认值是 0xFFFFFFFF 代表 SHOW_ALL 常量.
常量 数字值 描述
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 TreeWalker 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 TreeWalker 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 TreeWalker with a 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.
filter 可选
是一个实现了 NodeFilter 接口的对象; its acceptNode() method will be called for each node in the subtree based at root which is accepted as included by the whatToShow flag to determine whether or not to include it in the list of iterable nodes (你可以使用一个简单的回掉函数代替该方法). 该方法必须返回下列常量之一: NodeFilter.FILTER_ACCEPT, NodeFilter.FILTER_REJECT, or NodeFilter.FILTER_SKIP. See the Example.
Note: Prior to Gecko 12.0 (Firefox 12.0 / Thunderbird 12.0 / SeaMonkey 2.9), this method accepted an optional fourth parameter (entityReferenceExpansion) that is not part of the DOM4 specification, and has therefore been removed. This parameter indicated whether or not the children of entity reference nodes were visible to the iterator. Since such nodes were never created in browsers, this paramater had no effect.

实例

// 用于返回body元素下的所有p标签,并保存在pars列表中
var nodeIterator = document.createNodeIterator(
    document.body,
    NodeFilter.SHOW_ELEMENT,
    function(node) {
        return node.nodeName.toLowerCase() === 'p' ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;
    }
);
var pars = [];
var currentNode;

while (currentNode = nodeIterator.nextNode()) {
  pars.push(currentNode);
}

浏览器兼容性

Supported in FF 3.5+, Chrome 1+, Opera 9+, Safari 3+, IE9+

参见

规范

DOM Level 2 Traversal: NodeIterator

文档标签和贡献者

 最后编辑者: caoruiy,