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 1 ✦ Generic HTML Element Objects (Chapter 15)<br />

<br />

<br />

<br />

addBehavior() and removeBehavior() Method Lab<br />

<br />

This is a sample paragraph. After turning on the behavior,<br />

it will turn your selected color when you mouse down anywhere in this<br />

paragraph.<br />

<br />

<br />

Choose a ‘hot’ color:<br />

<br />

red<br />

blue<br />

cyan<br />

<br />

<br />

<br />

<br />

<br />

To turn off the behavior, the removeBehavior() method is invoked. Notice that<br />

the removeBehavior() method is associated with the myP object, and the parameter<br />

is the ID of the behavior added earlier. If you associate multiple behaviors with<br />

an object, you can remove one without disturbing the others because each has its<br />

own unique ID.<br />

addEventListener(“eventType”, listenerFunc,<br />

useCapture)<br />

removeEventListener(“eventType”,<br />

listenerFunc, useCapture)<br />

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

Compatibility ✓<br />

Example<br />

Listing 15-20 provides a compact workbench to explore and experiment with the<br />

basic W3C DOM event model. When the page loads, no event listeners are registered<br />

with the browser (except for the control buttons, of course). But you can add<br />

an event listener for a click event in bubble and/or capture mode to the BODY element<br />

or the P element that surrounds the SPAN holding the line of text. If you add<br />

an event listener and click the text, you see a readout of the element processing the<br />

53<br />

elementObject.addEventListener()

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

Saved successfully!

Ooh no, something went wrong!