15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

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 4 ✦ The Document and Body Objects (Chapter 18)<br />

The Results box shows that an object was created. Now, look at the properties of<br />

the object by typing a into the bottom text field. The precise listings of properties<br />

varies between IE5+ and NN6, but the W3C DOM properties that they share in common<br />

indicate that the object is a node type 3 with a node name of #text. No parents,<br />

children, or siblings exist yet because the object created here is not part of<br />

the document hierarchy tree until it is explicitly added to the document.<br />

To see how insertion works, enter the following statement into the top text field<br />

to append the text node to the myP paragraph:<br />

document.getElementById(“myP”).appendChild(a)<br />

The word “Hello” appears at the end of the simple paragraph lower on the page.<br />

Now you can modify the text of that node either via the reference from the point of<br />

view of the containing P element or via the global variable reference for the newly<br />

created node:<br />

document.getElementById(“myP”).lastChild.nodeValue = “Howdy”<br />

or<br />

a.nodeValue = “Howdy”<br />

elementFromPoint(x, y)<br />

NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5<br />

Compatibility ✓ ✓ ✓<br />

Example<br />

Listing 18-14 is a document that contains many different types of elements, each<br />

of which has an ID attribute assigned to it. The onMouseOver event handler for the<br />

document object invokes a function that finds out which element the cursor is over<br />

when the event fires. Notice that the event coordinates are event.clientX and<br />

event.clientY, which use the same coordinate plane as the page for their point of<br />

reference. As you roll the mouse over every element, its ID appears on the page. In<br />

Figure 4-1, the pointer is inside a table cell, whose ID appears in bold at the end of<br />

the first paragraph. Some elements, such as BR and TR, occupy no space in the document,<br />

so you cannot get their IDs to appear. On a typical browser screen size, a<br />

positioned element rests atop one of the paragraph elements so that you can see<br />

how the elementFromPoint() method handles overlapping elements. If you scroll<br />

the page, the coordinates for the event and the page’s elements stay in sync.<br />

247<br />

document.elementFromPoint()

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

Saved successfully!

Ooh no, something went wrong!