Main Page

previousNode

var node3 = iterator.previousNode();
alert(node1 == node3); ///outputs “true”
For example, suppose you wanted to list all elements contained within a specific
<div/>
inside of a
specified area on an HTML page. The following code accomplishes this:
<html>
<head>
<title>NodeIterator Example</title>
<script type=”text/javascript”>
var iterator = null;
function makeList() {
var oDiv = document.getElementById(“div1”);
iterator = document.createNodeIterator(oDiv,
NodeFilter.SHOW_ELEMENT, null, false);
var oOutput = document.getElementById(“text1”);
var oNode = iterator.nextNode();
while (oNode) {
oOutput.value += oNode.tagName + “\n”;
oNode = iterator.nextNode();
}
}
</script>
</head>
<body>
<div id=”div1”>
<p>Hello <b>World!</b></p>
<ul>
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
</ul>
</div>
<textarea rows=”10” cols=”40” id=”text1”></textarea><br />
<input type=”button” value=”Make List” onclick=”makeList()” /> </body>
</html>
When the button is clicked, the
<textarea/>
is filled with the tag names of the elements contained in
div1
:
P
B
UL
LI
LI
LI
But suppose you don’t want to include
<p/>
elements in the results. This can’t be accomplished just by
using the
whatToShow
argument. In this case, you need a custom
NodeFilter
object.
185
DOM Basics
09_579088 ch06.qxd 3/28/05 11:37 AM Page 185


JavaScript EditorFree JavaScript Editor     Ajax Editor


©