23.04.2013 Views

javascript

javascript

javascript

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 8 ■ SCRIPTING CSS<br />

318<br />

Don’t feel bad. I didn’t think you’d get that one right. What’s going on here? Any declarations you<br />

write to cssText totally overwrite those already in there. So, our declarations for left, top, padding-left,<br />

and height disappeared. This in turn means that the declarations for those properties in eight.css now<br />

shine through. Therefore, other than swapping the blue sprite for the fuchsia one, our brooks link is<br />

styled the same as when the page initially loaded. This is equivalent to the following markup:<br />

Brooks<br />

Note that when you write the value of cssText, property names are dash case, not camel case—just<br />

like in the CSS standard.<br />

Scripting Classes<br />

Writing CSS2Properties members or cssText provides a way to restyle one element, but what if you want<br />

to do so for several elements? One way is to script className, typically of an ancestor element.<br />

Remember that for any element, the className member represents the class attribute.<br />

In our included style sheet, eight.css, we have a rule for a blue class and a fuchsia class. The latter is<br />

dormant. That is to say, no element in our markup is a member of the fuchsia class. So, refresh Firefox,<br />

clear Firebug, and let’s change the className member for the element from blue to fuchsia. Doing<br />

so, as Figure 8–6 illustrates, swaps the sprite for all six links in one fell swoop:<br />

document.getElementsByTagName("ul")[0].className = "fuchsia";<br />

Figure 8–6. Writing the className member to swap the blue sprite for the fuchsia one

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

Saved successfully!

Ooh no, something went wrong!