04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 11: DOM Levels 2 and 3<br />

var supportsTraversals = document.implementation.hasFeature(“Traversal”, “2.0”);<br />

var supportsNodeIterator = (typeof document.createNodeIterator == “function”);<br />

var supportsTreeWalker = (typeof document.createTreeWalker == “function”);<br />

As stated previously, DOM traversals are a depth - first traversal of the DOM structure that allows<br />

movement in at least two directions (depending on the type being used). A traversal is rooted at a given<br />

node, and it cannot go any further up the DOM tree than that root. Consider the following HTML page:<br />

< html ><br />

< head ><br />

< title > Example < /title ><br />

< /head ><br />

< body ><br />

< p > < b > Hello < /b > world! < /p ><br />

< /body ><br />

< /html ><br />

This page evaluates to the DOM tree represented in Figure 11 - 4 .<br />

Document<br />

Element html<br />

Element head<br />

Element body<br />

Element title<br />

Element p<br />

Text Example<br />

Element b<br />

Text world!<br />

Figure 11-4<br />

Text Hello<br />

Any node can be the root of the traversals. Suppose, for example, that the < body > element is the<br />

traversal root. The traversal can then visit the < p > element, the < b > element, and the two text nodes that<br />

are descendants of < body > ; however, the traversal can never reach the < html > element, the < head ><br />

element, or any other node that isn ’ t in the < body > element ’ s subtree. A traversal that has its root at<br />

document , on the other hand, can access all of the nodes in document. Figure 11-5 depicts a depth - first<br />

traversal of a DOM tree rooted at document.<br />

343

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!