TRAVERSALS
The DOM Level 2 Traversal and Range module defines two types that aid in sequential traversal of a DOM structure. These types, NodeIterator
and TreeWalker
, perform depth-first traversals of a DOM structure given a certain starting point.
As stated previously, DOM traversals are a depth-first traversal of the DOM structure that allows movement in at least two directions (depending on the type being used). A traversal is rooted at a given node, and it cannot go any further up the DOM tree than that root. Consider the following HTML page:
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
<p><b>Hello</b> world!</p>
</body>
</html>
This page evaluates to the DOM tree represented in Figure 16-4.
Any node can be the root of the traversals. Suppose, for example, that the <body>
element is the traversal root. The traversal can then visit the <p>
element...