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 9 ■ LISTENING FOR EVENTS<br />

358<br />

As its name implies, ElementTraversal is designed for traversing Element nodes. So,<br />

firstElementChild, lastElementChild, previousElementSibling, and nextElementSibling will contain an<br />

Element node or null (and never a Text, Comment, or any other kind of node). Note that<br />

childElementCount differs from childNodes.length in that it contains just the number of child Element<br />

nodes rather than the overall number of child nodes.<br />

Therefore, for Internet Explorer 9, Firefox, Safari, Chrome, and Opera, we can rework<br />

traverseTree() with firstElementChild and nextElementSibling to replace firstChild and nextSibling.<br />

The two function literals in the ?: expression differ only by those identifiers:<br />

var traverseTree = document.documentElement.firstElementChild ?<br />

function traverseTree (node, func) {<br />

func(node);<br />

node = node.firstElementChild;<br />

while (node !== null) {<br />

traverseTree(node, func);<br />

node = node.nextElementSibling;<br />

}<br />

} :<br />

function traverseTree (node, func) {<br />

func(node);<br />

node = node.firstChild;<br />

while (node !== null) {<br />

traverseTree(node, func);<br />

node = node.nextSibling;<br />

}<br />

} ;<br />

Note that the boolean expression, document.documentElement.firstElementChild, queries<br />

firstElementChild for the Element node. That would be the Element node.<br />

Why not simply query the firstElementChild of the Document node? We could see whether<br />

document.firstElementChild refers to the Element node. That’s a bad idea: ElementTraversal is<br />

implemented only by Element nodes. So although all 12 node types have a firstChild member, only an<br />

Element node has a firstElementChild member, too.<br />

Hmm. If it were up to me, a Document node would implement ElementTraversal, too. But I do not<br />

write the standards, just about them.<br />

■ Note If you want to wade through the details of the ElementTraversal interface, visit<br />

www.w3.org/TR/ElementTraversal/.<br />

Finding an Element by Class<br />

The next helper function we want to rework is findClass(), which we wrote in Chapter 7 to help us find<br />

an Element node by its class attribute. DOM 3 adds a NodeSelector interface that defines two methods,<br />

querySelectorAll() and querySelector(). Those provide a way to query Element nodes with CSS<br />

selectors. That is to say, querySelectorAll() and querySelector() enable you to query elements in the<br />

same way that you would target them in a CSS rule. So for example, to query the element in our<br />

markup, we could invoke querySelectorAll() in any of the following ways in Firebug:

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

Saved successfully!

Ooh no, something went wrong!