Document.createTreeWalker()
creator method returns a newly created
TreeWalker
对象。
document.createTreeWalker(root, whatToShow[, filter[, entityReferenceExpansion]]);
root
节点
of this
TreeWalker
traversal. Typically this will be an element owned by the document.
whatToShow
可选
unsigned long
representing a bitmask created by combining the constant properties of
NodeFilter
. It is a convenient way of filtering for certain types of node. It defaults to
0xFFFFFFFF
表示
SHOW_ALL
常量。
| 常量 | 数值 | 描述 |
NodeFilter.SHOW_ALL
|
-1
(that is the max value of
unsigned long
)
|
展示所有节点。 |
NodeFilter.SHOW_ATTRIBUTE
|
2
|
Shows attribute
Attr
nodes. This is meaningful only when creating a
TreeWalker
采用
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
|
展示
CDATASection
节点。
|
NodeFilter.SHOW_COMMENT
|
128
|
展示
注释
节点。
|
NodeFilter.SHOW_DOCUMENT
|
256
|
展示
Document
节点。
|
NodeFilter.SHOW_DOCUMENT_FRAGMENT
|
1024
|
展示
DocumentFragment
节点。
|
NodeFilter.SHOW_DOCUMENT_TYPE
|
512
|
展示
DocumentType
节点。
|
NodeFilter.SHOW_ELEMENT
|
1
|
展示
元素
节点。
|
NodeFilter.SHOW_ENTITY
|
32
|
展示
Entity
nodes. This is meaningful only when creating a
TreeWalker
采用
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
nodes. This is meaningful only when creating a
TreeWalker
采用
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
|
展示
文本
节点。
|
filter
可选
NodeFilter
, that is an object with a method
acceptNode
, which is called by the
TreeWalker
to determine whether or not to accept a node that has passed the
whatToShow
check.
entityReferenceExpansion
可选
布尔
flag indicating if when discarding an
EntityReference
its whole sub-tree must be discarded at the same time.
新的
TreeWalker
对象。
The following example goes through all nodes in the body, reduces the set of nodes to elements, simply passes through as acceptable each node (it could reduce the set in the
acceptNode()
method instead), and then makes use of tree walker iterator that is created to advance through the nodes (now all elements) and push them into an array.
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
The definition of 'Document.createTreeWalker' in that specification. |
实时标准 |
移除
expandEntityReferences
parameter. Made the
whatToShow
and
filter
parameters optionals.
|
|
Document Object Model (DOM) Level 2 Traversal and Range Specification
The definition of 'Document.createTreeWalker' in that specification. |
过时 | 初始定义 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
createTreeWalker
|
Chrome 1 | Edge 12 | Firefox 2 | 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 |
expandEntityReferences
参数
|
Chrome 4 | Edge 12 | Firefox 2 — 12 | IE 9 | Opera 9 | Safari 3 | WebView Android Yes | Chrome Android Yes | Firefox Android 4 — 14 | Opera Android Yes | Safari iOS 3 | Samsung Internet Android Yes |
whatToShow
and
filter
optional
|
Chrome 4 | Edge ≤79 | Firefox 12 | IE No | Opera Yes | Safari 3 | WebView Android Yes | Chrome Android Yes | Firefox Android 14 | Opera Android Yes | Safari iOS 3 | Samsung Internet Android Yes |
完整支持
不支持
TreeWalker
.
Document
alinkColor
all
anchors
applets
bgColor
body
characterSet
childElementCount
children
compatMode
contentType
currentScript
defaultView
designMode
dir
doctype
documentElement
documentURI
documentURIObject
domain
domConfig
嵌入
fgColor
firstElementChild
forms
fullscreen
fullscreenEnabled
head
height
hidden
图像
实现
lastElementChild
lastModified
lastStyleSheetSet
linkColor
链接
location
mozSyntheticDocument
onabort
onafterscriptexecute
onanimationcancel
onanimationend
onanimationiteration
onauxclick
onbeforescriptexecute
onblur
oncancel
oncanplay
oncanplaythrough
onchange
onclick
onclose
oncontextmenu
oncuechange
ondblclick
ondurationchange
onended
onerror
onfocus
onformdata
onfullscreenchange
onfullscreenerror
ongotpointercapture
oninput
oninvalid
onkeydown
onkeypress
onkeyup
onload
onloadeddata
onloadedmetadata
onloadend
onloadstart
onlostpointercapture
onmousedown
onmouseenter
onmouseleave
onmousemove
onmouseout
onmouseover
onmouseup
onoffline
ononline
onpause
onplay
onplaying
onpointercancel
onpointerdown
onpointerenter
onpointerleave
onpointermove
onpointerout
onpointerover
onpointerup
onreset
onresize
onscroll
onselect
onselectionchange
onselectstart
onsubmit
ontouchcancel
ontouchstart
ontransitioncancel
ontransitionend
onvisibilitychange
onwheel
origin
plugins
popupNode
preferredStyleSheetSet
readyState
referrer
rootElement
脚本
scrollingElement
selectedStyleSheetSet
styleSheetSets
timeline
title
tooltipNode
URL
visibilityState
vlinkColor
width
xmlEncoding
xmlVersion
adoptNode()
append()
caretRangeFromPoint()
clear()
close()
createAttribute()
createCDATASection()
createComment()
createDocumentFragment()
createElement()
createElementNS()
createEntityReference()
createEvent()
createExpression()
createExpression()
createNodeIterator()
createNSResolver()
createNSResolver()
createProcessingInstruction()
createRange()
createTextNode()
createTouch()
createTouchList()
createTreeWalker()
enableStyleSheetsForSet()
evaluate()
evaluate()
execCommand()
exitFullscreen()
exitPointerLock()
getAnimations()
getBoxObjectFor()
getElementById()
getElementsByClassName()
getElementsByName()
getElementsByTagName()
getElementsByTagNameNS()
hasFocus()
hasStorageAccess()
importNode()
mozSetImageElement()
open()
prepend()
queryCommandEnabled()
queryCommandSupported()
querySelector()
querySelector()
querySelectorAll()
querySelectorAll()
registerElement()
releaseCapture()
replaceChildren()
requestStorageAccess()
write()
writeln()
animationcancel
animationend
animationiteration
animationstart
copy
cut
DOMContentLoaded
drag
dragend
dragenter
dragexit
dragleave
dragover
dragstart
drop
fullscreenchange
fullscreenerror
gotpointercapture
keydown
keypress
keyup
lostpointercapture
paste
pointercancel
pointerdown
pointerenter
pointerleave
pointerlockchange
pointerlockerror
pointermove
pointerout
pointerover
pointerup
readystatechange
scroll
selectionchange
selectstart
touchcancel
touchend
touchmove
touchstart
transitioncancel
transitionend
transitionrun
transitionstart
visibilitychange
wheel