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.

Figure 13-2: Clicking on one SPAN element highlights fellow class<br />

members.<br />

Chapter 13 ✦ Event Objects (Chapter 29)<br />

Notice that the scripts don’t have to know anything about the objects on the<br />

page to address each clicked one individually. That’s because the srcElement<br />

property provides all of the specificity needed for acting on the target element.<br />

Listing 29-17: Using the srcElement property<br />

<br />

<br />

srcElement Property<br />

<br />

.bold {font-weight:bold}<br />

.ital {font-style:italic}<br />

<br />

<br />

function highlight() {<br />

var elem = event.srcElement<br />

if (elem.className == “bold”) {<br />

document.styleSheets[0].rules[0].style.color = “red”<br />

} else {<br />

elem.style.color = “#FFCC00”<br />

}<br />

}<br />

function restore() {<br />

var elem = event.srcElement<br />

if (elem.className == “bold”) {<br />

Continued<br />

421<br />

(IE) event.srcElement

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

Saved successfully!

Ooh no, something went wrong!