document.createTreeWalker()

翻译不完整。 请帮助我们翻译这篇文章!

document.createTreeWalker() 方法,能创建并返回一个 TreeWalker 对象。

语法

treeWalker = document.createTreeWalker(root, whatToShow, filter, entityReferenceExpansion);

参数

root
是遍历这个 TreeWalker 的根节点(root Node)。通常这是文档的一个元素。
whatToShow 可选
一个无符号长整型,表示一个整合自 NodeFilter 常量属性的位掩码。这是筛选特定类型节点的便捷方式。默认为 0xFFFFFFFF,表示 SHOW_ALL 常量。
常量 数值 描述
NodeFilter.SHOW_ALL -1 (that is the max value of unsigned long) 显示所有节点。
NodeFilter.SHOW_ATTRIBUTE 2 显示特性Attr节点。这只在当以一个特性节点Attr为起点节点的TreeWalker 中有意义;在这种情况下,这意味着特性节点会出现在迭代或遍历第一次出现的位置。因为特性节点没有其他节点一样的子节点,所以在文档树中的遍历不会出现特性节点。
NodeFilter.SHOW_CDATA_SECTION 8 显示CDTA CDATASection 节点.
NodeFilter.SHOW_COMMENT 128 显示注释Comment节点
NodeFilter.SHOW_DOCUMENT 256 显示文档Document节点
NodeFilter.SHOW_DOCUMENT_FRAGMENT 1024 显示文档片段DocumentFragment节点
NodeFilter.SHOW_DOCUMENT_TYPE 512 显示文档类型DocumentType节点
NodeFilter.SHOW_ELEMENT 1 显示元素Element节点
NodeFilter.SHOW_ENTITY 32 显示实体Entity节点。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 显示实体引用 EntityReference 节点。
NodeFilter.SHOW_NOTATION 2048 显示符号Notation 节点。 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 显示处理指令 ProcessingInstruction节点。
NodeFilter.SHOW_TEXT 4 顯示文字節點(Text nodes).
filter 可选
一个可选的 NodeFilter,即一个具有 acceptNode 方法的对象,此方法被 TreeWalker 调用以决定是否接受已通过 whatToShow 检查的节点。
entityReferenceExpansion 可选
一个 Boolean 标识,指示当丢弃一个 EntityReference 时是否同时丢弃其子树。

返回值

一个新的 TreeWalker 对象。

示例

以下示例遍历 body 下的所有节点,将节点集合缩小至元素,简单地传递每个可接受的节点(也可在 acceptNode() 方法中缩小集合),然后利用创建的 TreeWalker 迭代器在节点上推进(现在是所有的元素)并把它们推入一个数组。

var treeWalker = document.createTreeWalker(
  document.body,
  NodeFilter.SHOW_ELEMENT,
  { acceptNode: function(node) { return NodeFilter.FILTER_ACCEPT; } },
  false
);

var nodeList = [];
var currentNode = treeWalker.currentNode;

while(currentNode) {
  nodeList.push(currentNode);
  currentNode = treeWalker.nextNode();
}

规范

规范 状态 备注
DOM
Document.createTreeWalker
Living Standard Removed the expandEntityReferences parameter.
Made the whatToShow and filter parameters optionals.
Document Object Model (DOM) Level 2 Traversal and Range Specification
Document.createTreeWalker
Obsolete Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
createTreeWalkerChrome Full support 1Edge Full support 12Firefox Full support 2IE Full support 9Opera Full support 9Safari Full support 3WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 3Samsung Internet Android Full support Yes
expandEntityReferences parameterChrome Full support 4Edge Full support 12Firefox No support 2 — 12IE Full support 9Opera Full support 9Safari Full support 3WebView Android Full support YesChrome Android Full support YesFirefox Android No support 4 — 14Opera Android Full support YesSafari iOS Full support 3Samsung Internet Android Full support Yes
whatToShow and filter optionalChrome Full support 4Edge Full support ≤79Firefox Full support 12IE No support NoOpera Full support YesSafari Full support 3WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 14Opera Android Full support YesSafari iOS Full support 3Samsung Internet Android Full support Yes

Legend

Full support  
Full support
No support  
No support

参见