TreeWalker

TreeWalker 对象用于表示文档子树中的节点和它们的位置。

TreeWalker 可以使用 Document.createTreeWalker() 方法创建。

属性

这个接口不继承任何属性。

TreeWalker.root 只读
返回一个 Node ,表示新建 TreeWalker 时所声明的根节点。
TreeWalker.whatToShow 只读
返回一个 unsigned long 类型的常量位掩码,表示需要筛选的Node 类型。不匹配的节点会跳过,但其子节点,如果符合条件,则也会被包含。可能的值如下:
常量 数字值 描述
NodeFilter.SHOW_ALL -1 (that is the max value of unsigned long) 显示所有节点。
NodeFilter.SHOW_ATTRIBUTE 2 显示Attr节点,这意味着使用TreeWalker访问 Attr节点时,需要让这些节点处于遍历的开始位置。这是因为这些节点不是任何节点的后代,并不处于文档树之上。
NodeFilter.SHOW_CDATA_SECTION 8 显示 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节点,这意味着使用TreeWalker访问 Entity节点时,需要让这些节点处于遍历的开始位置。这是因为这些节点不是任何节点的后代,并不处于文档树之上。
NodeFilter.SHOW_ENTITY_REFERENCE 16 显示 EntityReference 节点。
NodeFilter.SHOW_NOTATION 2048 显示Notation节点,这意味着使用TreeWalker访问 Notation节点时,需要让这些节点处于遍历的开始位置。这是因为这些节点不是任何节点的后代,并不处于文档树之上。
NodeFilter.SHOW_PROCESSING_INSTRUCTION 64 显示 ProcessingInstruction 节点。
NodeFilter.SHOW_TEXT 4 显示 Text 节点。
TreeWalker.filter 只读
返回一个实现NodeFilter接口的对象,这个对象用来挑选相关的节点。
TreeWalker.expandEntityReferences 只读
是个Boolean的标记,表明是否在丢弃一个EntityReference是否同时丢弃其后代。
TreeWalker.currentNode
返回 TreeWalker   当前指向的Node

方法

这个接口不继承任何方法。

注意:对于TreeWalker ,一个节点是否可见只取决于whatToShow 和 filter  两个参数(和元素是否在屏幕上可见无关)

TreeWalker.parentNode()
移动当前 Node到文档顺序中的第一个“可见”的祖先节点,并返回该节点。如果没有这样的节点,则会返回 null  ,同时也不会发生移动。
TreeWalker.firstChild()
移动当前 Node到当前节点的第一个“可见”子节点,并返回该节点。如果没有这样的节点,则会返回 null  ,同时也不会发生移动。
TreeWalker.lastChild()
移动当前 Node到当前节点的最末一个“可见”子节点,并返回该节点。如果没有这样的节点,则会返回 null  ,同时也不会发生移动。
TreeWalker.previousSibling()
移动当前 Node到当前节点的前一个兄弟节点,并返回该节点。如果没有这样的节点,则会返回 null  ,同时也不会发生移动。
TreeWalker.nextSibling()
移动当前 Node到当前节点的后一个兄弟节点,并返回该节点。如果没有这样的节点,则会返回 null  ,同时也不会发生移动。
TreeWalker.previousNode()
移动当前 Node到文档顺序中前一个节点,并返回该节点。如果没有这样的节点,则会返回 null  ,同时也不会发生移动。
TreeWalker.nextNode()
移动当前 Node到文档顺序中下一个节点,并返回该节点。如果没有这样的节点,则会返回 null  ,同时也不会发生移动。

规范

规范 状态 注释
DOM
TreeWalker
Living Standard Removed the expandEntityReferences property.
Document Object Model (DOM) Level 2 Traversal and Range Specification
TreeWalker
Obsolete Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
TreeWalkerChrome Full support 1Edge Full support 12Firefox Full support 4IE Full support 9Opera Full support 9Safari Full support 3WebView Android Full support 3Chrome Android Full support YesFirefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 3Samsung Internet Android Full support Yes
currentNodeChrome Full support 1Edge Full support 12Firefox Full support 4IE Full support 9Opera Full support 9Safari Full support 3WebView Android Full support 3Chrome Android Full support YesFirefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 3Samsung Internet Android Full support Yes
expandEntityReferences
Deprecated
Chrome Full support 1Edge Full support 12Firefox No support 4 — 21IE Full support 9Opera Full support 9Safari Full support 3WebView Android Full support 3Chrome Android Full support YesFirefox Android No support 4 — 21Opera Android Full support 10.1Safari iOS Full support 3Samsung Internet Android Full support Yes
filterChrome Full support 1Edge Full support 12Firefox Full support 4IE Full support 9Opera Full support 9Safari Full support 3WebView Android Full support 3Chrome Android Full support YesFirefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 3Samsung Internet Android Full support Yes
firstChildChrome Full support 1Edge Full support 12Firefox Full support 4IE Full support 9Opera Full support 9Safari Full support 3WebView Android Full support 3Chrome Android Full support YesFirefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 3Samsung Internet Android Full support Yes
lastChildChrome Full support 1Edge Full support 12Firefox Full support 4IE Full support 9Opera Full support 9Safari Full support 3WebView Android Full support 3Chrome Android Full support YesFirefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 3Samsung Internet Android Full support Yes
nextNodeChrome Full support 1Edge Full support 12Firefox Full support 4IE Full support 9Opera Full support 9Safari Full support 3WebView Android Full support 3Chrome Android Full support YesFirefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 3Samsung Internet Android Full support Yes
nextSiblingChrome Full support 1Edge Full support 12Firefox Full support 4IE Full support 9Opera Full support 9Safari Full support 3WebView Android Full support 3Chrome Android Full support YesFirefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 3Samsung Internet Android Full support Yes
parentNodeChrome Full support 1Edge Full support 12Firefox Full support 4IE Full support 9Opera Full support 9Safari Full support 3WebView Android Full support 3Chrome Android Full support YesFirefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 3Samsung Internet Android Full support Yes
previousNodeChrome Full support 1Edge Full support 12Firefox Full support 4IE Full support 9Opera Full support 9Safari Full support 3WebView Android Full support 3Chrome Android Full support YesFirefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 3Samsung Internet Android Full support Yes
previousSiblingChrome Full support 1Edge Full support 12Firefox Full support 4IE Full support 9Opera Full support 9Safari Full support 3WebView Android Full support 3Chrome Android Full support YesFirefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 3Samsung Internet Android Full support Yes
rootChrome Full support 1Edge Full support 12Firefox Full support 4IE Full support 9Opera Full support 9Safari Full support 3WebView Android Full support 3Chrome Android Full support YesFirefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 3Samsung Internet Android Full support Yes
whatToShowChrome Full support 1Edge Full support 12Firefox Full support 4IE Full support 9Opera Full support 9Safari Full support 3WebView Android Full support 3Chrome Android Full support YesFirefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 3Samsung Internet Android Full support Yes

Legend

Full support  
Full support
No support  
No support
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.

参见