23.04.2013 Views

javascript

javascript

javascript

SHOW MORE
SHOW LESS

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

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

CHAPTER 7 ■ TRAVERSING AND MODIFYING THE DOM TREE<br />

270<br />

Similarly, querying previousSibling on the first child node of the returns null, too:<br />

var myUL = document.<br />

childNodes[1].<br />

childNodes[1].<br />

childNodes[1].<br />

childNodes[3];<br />

myUL.childNodes[0].previousSibling;<br />

// null<br />

Now then, is there a simpler way to query the last child node of the than keying in<br />

childNodes[myUL.childNodes.length - 1]? Oh, you betcha. Just query the aptly named lastChild<br />

member:<br />

var myUL = document.<br />

childNodes[1].<br />

childNodes[1].<br />

childNodes[1].<br />

childNodes[3];<br />

myUL.lastChild.previousSibling;<br />

// <br />

Conversely, every kind of node has a firstChild member referring to its first child node. So rather<br />

than querying childNodes[0], you can save a few keystrokes with firstChild. Note that, in addition to<br />

saving keystrokes, lastChild and firstChild read better than their childNodes equivalents.<br />

So, click Clear in both Firebug panels and give firstChild a try, verifying this and the previous<br />

sample with Figure 7–7:<br />

var myUL = document.<br />

childNodes[1].<br />

childNodes[1].<br />

childNodes[1].<br />

childNodes[3];<br />

myUL.firstChild.nextSibling;<br />

//

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

Saved successfully!

Ooh no, something went wrong!