Document.createTreeWalker()
Document.createTreeWalker() возвращает новый объект класса TreeWalker (en-US).Синтаксис
document.createTreeWalker(root, whatToShow[, filter[, entityReferenceExpansion]]);
Параметры
root-
корневой узел
NodeдляTreeWalker(en-US). Чаще всего это элемент принадлежащий document. whatToShowНеобязательный-
A
unsigned longrepresenting a bitmask created by combining the constant properties ofNodeFilter. It is a convenient way of filtering for certain types of node. It defaults to0xFFFFFFFFrepresenting theSHOW_ALLconstant.Константа Числовое значение Описание NodeFilter.SHOW_ALL-1(that is the max value ofunsigned long)Показывать все узлы. NodeFilter.SHOW_ATTRIBUTEУстарело2Shows attribute Attrnodes. This is meaningful only when creating aTreeWalker(en-US) with anAttrnode 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Устарело8Shows CDATASectionnodes.NodeFilter.SHOW_COMMENT128Shows Commentnodes.NodeFilter.SHOW_DOCUMENT256Shows Documentnodes.NodeFilter.SHOW_DOCUMENT_FRAGMENT1024Shows DocumentFragmentnodes.NodeFilter.SHOW_DOCUMENT_TYPE512Shows DocumentType(en-US) nodes.NodeFilter.SHOW_ELEMENT1Shows Elementnodes.NodeFilter.SHOW_ENTITYУстарело32Shows Entitynodes. This is meaningful only when creating aTreeWalker(en-US) with anEntitynode as its root; in this case, it means that theEntitynode 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Устарело16Shows EntityReferencenodes.NodeFilter.SHOW_NOTATIONУстарело2048Shows Notationnodes. This is meaningful only when creating aTreeWalker(en-US) with aNotationnode as its root; in this case, it means that theNotationnode 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_INSTRUCTION64Shows ProcessingInstruction(en-US) nodes.NodeFilter.SHOW_TEXT4Shows Textnodes. filterНеобязательный-
A
NodeFilter(en-US), that is an object with a methodacceptNode, which is called by theTreeWalker(en-US) to determine whether or not to accept a node that has passed thewhatToShowcheck. entityReferenceExpansionНеобязательный-
A
Boolean(en-US) flag indicating if when discarding anEntityReferenceits whole sub-tree must be discarded at the same time.
Return value
A new TreeWalker (en-US) object.
Example
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.
js
var treeWalker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_ELEMENT,
{ acceptNode: function(node) { return NodeFilter.FILTER_ACCEPT; } },
false
);
var nodeList = [];
while(treeWalker.nextNode()) nodeList.push(treeWalker.currentNode);
Specifications
| Specification | Status | Comment |
|---|---|---|
| DOM Определение 'Document.createTreeWalker' в этой спецификации. |
Живой стандарт | Removed the expandEntityReferences parameter. Made the whatToShow and filter parameters optionals. |
| Document Object Model (DOM) Level 2 Traversal and Range Specification Определение 'Document.createTreeWalker' в этой спецификации. |
Устаревшая | Initial definition |
Browser compatibility
BCD tables only load in the browser
See also
- The interface of the object it creates:
TreeWalker(en-US).