How to create a child node iterator

Use: document.createNodeIterator (); passing a root node, return the root node of the sub-walker, and by which method: nextNode () and previousNode () traversing its child nodes;

var nodeIterator = document.createNodeIterator(
  document.body,
  NodeFilter.SHOW_ELEMENT
);

 

Wherein the second parameter indicates the type of the generated child nodes of the walker, since there are seven types of nodes, in addition to several child nodes can not be used as this parameter may be: 

1. All nodes: NodeFilter.SHOW_ALL

2. The element node: NodeFilter.SHOW_ELEMENT

3. Text node: NodeFilter.SHOW_TEXT

4. Note Node: NodeFilter.SHOW_COMMENT

 

Here is the specific wording of traversal: 

var nodeIterator = document.createNodeIterator(document.body);
var pars = [];
var currentNode;

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

 

var nodeIterator = document.createNodeIterator(
  document.body,
  NodeFilter.SHOW_ELEMENT
);

var currentNode = nodeIterator.nextNode();
var previousNode = nodeIterator.previousNode();

currentNode === previousNode // true

 

Guess you like

Origin www.cnblogs.com/aisowe/p/11547422.html