23.04.2013 Views

javascript

javascript

javascript

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

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

arguments.callee(node, func);<br />

node = node.nextSibling;<br />

}<br />

}<br />

function findClass(name, root) {<br />

var found = [];<br />

root = root || document.body;<br />

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

if (!! node.className) {<br />

for (var names = node.className.split(/\s+/), i = names.length; i --; ) {<br />

if (names[i] === name) {<br />

found.push(node);<br />

}<br />

}<br />

}<br />

});<br />

return found;<br />

}<br />

findClass("sprite");<br />

// [li#twitter.sprite, li#facebook.sprite, li#flickr.sprite, li#linkedin.sprite]<br />

Did JavaScript return an array containing your four elements? Good job. You’re not a JavaScript<br />

bumpkin anymore!<br />

Figure 7–13. Retrieving elements of the same class<br />

281

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

Saved successfully!

Ooh no, something went wrong!