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

286<br />

Now query some members from the Node interface. Remember to stop and click Run prior to each<br />

comment in order to verify the return value:<br />

myAttrNode.nodeType;<br />

// 11<br />

myAttrNode.nodeName;<br />

// "class"<br />

myAttrNode.nodeValue;<br />

// "sprite"<br />

Now query some members the Attr node received by way of the Attr interface:<br />

myAttrNode.name;<br />

// "class"<br />

myAttrNode.value;<br />

// "sprite"<br />

myAttrNode.value = "sprout";<br />

myAttrNode.value;<br />

// "sprout"<br />

myAttrNode.specified;<br />

// true<br />

So for an Attr node, Node.nodeName and Attr.name contain the same value, a string indicating the<br />

name of the attribute. Similarly, both Node.nodeValue and Attr.value contain the value of the attribute<br />

as a string. So, the first two members, name and value, are redundant. On the other hand, Attr.specified<br />

contains a boolean: true if you explicitly set the attribute in your markup or by JavaScript and false if<br />

not. So, false means the attribute value is a default from the document’s DTD. With those things in<br />

mind, querying Attr.specified will likely be the only time you work with an attribute through the Attr<br />

interface (as a node) rather than the Element interface (as a string).<br />

Enumerating Attributes for an Element<br />

For 11 of the 12 node types, the Node.attributes member simply contains null. But not for Element<br />

nodes. For those, Node.attributes contains a NamedNodeMap, which is an arraylike object. Remember that<br />

those contain numerically indexed members and a length member just like a genuine array but none of<br />

the handy array methods like Array.splice().<br />

What does Node.attributes contain? For every attribute explicitly set in your markup or by script,<br />

Node.attributes contains a corresponding Attr node. So, no default Attr nodes in there.<br />

Alrighty then, click Clear in both Firebug panels, and let’s explore Node.attributes:<br />

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

arrayOfAttrNodes.length;<br />

// 2<br />

So two Attr nodes appear, one for id and one for class. But there are no default ones like style or<br />

dir. Now then, the numerical indexes in a NamedNodeMap are there just for enumeration purposes. That is<br />

to say, DOM does not specify whether those should be ordered relative to source code, alphabetically, or<br />

by any other pattern. So, browsers will vary in their numbering. For example, id appears first in the<br />

Twitter but has an index of 1, not 0, in Firefox:<br />

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

arrayOfAttrNodes[1].name;<br />

// "id"

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

Saved successfully!

Ooh no, something went wrong!