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 />

But a NamedNodeMap is called a NamedNodeMap for a reason. You can, you know, query members by<br />

name, with an identifier and the . operator or with a string and the [] operator. Try both ways, verifying<br />

your work with Figure 7–15:<br />

var arrayOfAttrNodes = document.getElementById("twitter").attributes;<br />

arrayOfAttrNodes.id.value;<br />

// "twitter"<br />

var arrayOfAttrNodes = document.getElementById("twitter").attributes;<br />

arrayOfAttrNodes["class"].value;<br />

// "sprite"<br />

Figure 7–15. Querying attributes with refinement operators<br />

In regard to Node.attributes, Internet Explorer again says, “Ill be on my own side. By myself.” Prior<br />

to version 8, Internet Explorer put every default attribute from the DTD in an element’s attributes<br />

member. So, there might be like 100 in there. Yipes! Internet Explorer 8 does not have the bug.<br />

Let’s take a moment to sigh ruefully over this Internet Explorer bug. Then find a workaround for<br />

Internet Explorer 7 and earlier.<br />

Hmm.<br />

Why don’t we...<br />

No, that won’t work.<br />

I know, filter the Attr nodes in attributes by their specified member. Just throw away the ones<br />

with a value of false. Click Clear in both Firebug panels, and then define a helper function named<br />

filterDefaultAttrNodes() like so:<br />

function filterDefaultAttrNodes(elem) {<br />

var filtered = [];<br />

for (var i = 0, j = elem.attributes.length; i < j; i ++) {<br />

if (elem.attributes[i].specified) {<br />

filtered.push(elem.attributes[i]);<br />

}<br />

}<br />

return filtered;<br />

}<br />

287

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

Saved successfully!

Ooh no, something went wrong!