11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

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.

<br />

This paragraph has an em element in it.<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

Because of the fact that Opera and Mozilla-based browsers include white space in their DOM<br />

tree, you may notice that you have to press the Delete button a few more times in the preceding<br />

example to effect the same change as you would in IE. Despite the DOM being standard, we<br />

see that a subtle difference in interpretation of the standard can have significant consequences.<br />

Modifying Nodes<br />

Elements really cannot be directly modified, although their attributes certainly can. This may<br />

seem strange, but it makes perfect sense when you consider that elements contain text nodes.<br />

To effect a change, you really have to modify the text nodes themselves. For example, if you<br />

had<br />

This is a test<br />

you would use<br />

textNode = document.getElementById('p1').firstChild;<br />

to access the text node ―This is a test‖ within the paragraph element. Notice how we strung<br />

together the firstChild property with the method call. Experienced DOM programmers find that<br />

stringing methods and properties together like this helps avoid having to use numerous<br />

individual statements to access a particular item. Once the textNode has been retrieved we<br />

could access its length using its length property (which indicates the number of characters it<br />

contains), or even set its value using the data property.<br />

alert(textNode.length); // would return 14<br />

textNode.data = "I've been changed!";<br />

DOM Level 1 also defines numerous methods to operate on text nodes. <strong>The</strong>se are summarized<br />

in Table 10-5.

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

Saved successfully!

Ooh no, something went wrong!