You are here: Reference > JavaScript > client-side > HTML DOM > methods > createTreeWalker (document, XMLDocument)
createTreeWalker method (document, XMLDocument)
9 | ||||
Creates a TreeWalker object that can be used to navigate through the nodes in a subtree.
Note: The createTreeWalker method is supported in Internet Explorer from version 9.
The TreeWalker object represents the result of the createTreeWalker method as a tree.
The members of the TreeWalker object can be used to navigate through the resulting tree.
For a detailed description, please see the page for the TreeWalker object.
The createNodeIterator method and the NodeIterator object are similar to the createTreeWalker method and the TreeWalker object.
The main difference is that the NodeIterator object represents the result as an ordered list not as a tree.
Syntax:
You can find the related objects in the Supported by objects section below.
Parameters:
Reference to a node that will be the root node of the TreeWalker object. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Integer that specifies the type of nodes to show.
Predefined constants are available for the possible values of this parameter, in the scope of the NodeFilter interface (e.g. NodeFilter.SHOW_ALL).
The value can be any combination of the following integer constants with the bitwise OR operator (the value of a predefined constant appears in parentheses after the constant in hexadecimal form):
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Reference to a user-specified function that can filter nodes.
The method gets a node for checking and must return an integer value as a result.
Predefined constants are available for the possible results, in the scope of the NodeFilter interface (e.g. NodeFilter.FILTER_ACCEPT).
The result can be one of the following values (the value of a predefined constant appears in parentheses after the constant):
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Boolean that indicates whether the contents of EntityReference nodes are visible for the TreeWalker object.
One of the following values:
|
Return value:
Returns the newly created TreeWalker object.
Example HTML code 1:
This example illustrates the use of the createTreeWalker method:
|
||||
<head> <script type="text/javascript"> function ElementChecker (node) { if (node.tagName.toLowerCase () == 'button') { return NodeFilter.FILTER_ACCEPT; } return NodeFilter.FILTER_SKIP; } function FindFirstButton () { if (document.createTreeWalker) { walker = document.createTreeWalker (document, NodeFilter.SHOW_ELEMENT, ElementChecker, false); // get the first matching node var button = walker.firstChild (); alert ("The label of the first button element:\n" + button.textContent); } else { alert ("Your browser does not support the createTreeWalker method!"); } } </script> </head> <body> <button onclick="FindFirstButton ()">Find the first button tag with the TreeWalker object</button> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 2:
This example shows how to iterate through span elements with the TreeWalker object:
|
||||
<head> <script type="text/javascript"> function ElementChecker (node) { if (node.tagName.toLowerCase () == 'span') { return NodeFilter.FILTER_ACCEPT; } return NodeFilter.FILTER_SKIP; } function FindMainSections () { var contElem = document.getElementById ("content"); if (document.createTreeWalker) { walker = document.createTreeWalker (contElem, NodeFilter.SHOW_ELEMENT, ElementChecker, false); // get the first matching node var node = walker.firstChild (); while (node) { alert ("The contents of the section:\n " + node.innerHTML); node = walker.nextSibling (); } } else { alert ("Your browser does not support the createTreeWalker method!"); } } </script> </head> <body> <div id="content"> <span> <b>1. Section</b><br /> <span> <b>1.1. Subsection</b><br /> </span> </span> <span> <b>2.Section</b><br /> </span> </div> <br /><br /> <button onclick="FindMainSections ()">Find the main sections with the TreeWalker object</button> </body> |
||||
|
||||
Did you find this example helpful?
|
Supported by objects:
Related pages:
External links:
User Contributed Comments