02.06.2013 Views

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

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.

.css()<br />

CHAPTER 2 ■ COMMON JQUERY ACTIONS AND METHODS<br />

The .css() method works just like .attr(), except it applies to styling rules. To return a value, pass the<br />

name of the value as the only argument to the method; to set a value, pass both an attribute name <strong>and</strong> a<br />

new value for it. Like .attr(), multiple values can be set using JSON format.<br />

To change all elements with class foo to have red text <strong>and</strong> a yellow background, use the following:<br />

$(".foo").css({<br />

"color":"red",<br />

"background":"yellow"<br />

});<br />

This code, once executed, adds new style rules to the selected elements (see Figure 2-15).<br />

Figure 2-15. The document after adding CSS styling to elements with class foo<br />

Before reloading the page, retrieve the background value from elements with class foo using the<br />

following code:<br />

$(".foo").css("background");<br />

This will return the following:<br />

>>> $(".foo").css("background");<br />

"yellow none repeat scroll 0% 0%"<br />

55

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

Saved successfully!

Ooh no, something went wrong!