Object: TreeWalker
The TreeWalker object enables you to navigate the document tree or subtree. This object is similar to the NodeIterator objects, except that TreeWalker presents nodes in a tree-oriented view, rather than the list view presented by NodeIterator. This allows you to use TreeWalker to navigate directly to a parent or child, rather than simply up and down in the list using NodeIterator.
Create a TreeWalker using Document.createTreeWalker().
TreeWalker properties
TreeWalker methods
Method name |
Description |
Availability |
|---|---|---|
Moves the TreeWalker to the first visible child of the current node. |
4.6 or later |
|
Moves the TreeWalker to the last visible child of the current node. |
4.6 or later |
|
Moves the TreeWalker to the next visible node in the document order, relative to the current node. |
4.6 or later |
|
Moves the TreeWalker to the next sibling of the current node. |
4.6 or later |
|
Moves the TreeWalker to the closest visible ancestor node of the current node. |
4.6 or later |
|
Moves the TreeWalker to the previous visible node in the document order, relative to the current node. |
4.6 or later |
|
Moves the TreeWalker to the previous sibling of the current node. |
4.6 or later |
Method: TreeWalker.firstChild()
The firstChild() method moves the TreeWalker to the first visible child of the current node. The value of TreeWalker.currentNode is updated to reflect the TreeWalker’s new location in the tree.
If no visible children exist in the TreeWalker’s view, the value of TreeWalker.currentNode remains unchanged.
Method: TreeWalker.lastChild()
The lastChild() method moves the TreeWalker to the last visible child of the current node. The value of TreeWalker.currentNode is updated to reflect the TreeWalker’s new location in the tree.
If no visible children exist in the TreeWalker’s view, the value of TreeWalker.currentNode remains unchanged.
Method: TreeWalker.nextNode()
The nextNode() method moves the TreeWalker to the next visible Node in the document order, relative to the current node. The value of TreeWalker.currentNode is updated to reflect the TreeWalker’s new location in the tree.
If no node exists after the current node in the TreeWalker’s view, the value of TreeWalker.currentNode remains unchanged.
Method: TreeWalker.nextSibling()
The nextSibling() method moves the TreeWalker to the next sibling of the current node. The value of TreeWalker.currentNode is updated to reflect the TreeWalker’s new location in the tree.
If no visible sibling node exists after the current node in the TreeWalker’s view, the value of TreeWalker.currentNode remains unchanged.
Method: TreeWalker.parentNode()
The parentNode() method moves the TreeWalker to the closest visible ancestor node of the current node. The value of TreeWalker.currentNode is updated to reflect the TreeWalker’s new location in the tree.
If no visible ancestor node exists in the TreeWalker’s view, the value of TreeWalker.currentNode remains unchanged.
Method: TreeWalker.previousNode()
The previousNode() method moves the TreeWalker to the previous visible Node in the document order, relative to the current node. The value of TreeWalker.currentNode is updated to reflect the TreeWalker’s new location in the tree.
If no node exists before the current node in the TreeWalker’s view, the value of TreeWalker.currentNode remains unchanged.
Method: TreeWalker.previousSibling()
The previousSibling() method moves the TreeWalker to the previous sibling of the current node. The value of TreeWalker.currentNode is updated to reflect the TreeWalker’s new location in the tree.
If no visible sibling node exists before the current node in the TreeWalker’s view, the value of TreeWalker.currentNode remains unchanged.