03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

404 x CHAPTER 11 JQUERY<br />

MODIFYING THE DOM WITH JQUERY<br />

Once you have a matched set, you want to do someth<strong>in</strong>g with it. For example, you may want to apply<br />

a CSS class or style to the items <strong>in</strong> it. Or you may want to append some behavior to them, like<br />

add<strong>in</strong>g a click h<strong>and</strong>ler that fires some code when the items get clicked. You’ve already seen some<br />

examples of this us<strong>in</strong>g the css <strong>and</strong> b<strong>in</strong>d methods, but jQuery has a lot more to offer. In the next two<br />

sections, you see how to work with the various CSS methods <strong>and</strong> learn how to set up event h<strong>and</strong>lers.<br />

CSS Methods<br />

CSS is supported <strong>in</strong> jQuery <strong>in</strong> a few different ways. First, there’s the css method that enables you to<br />

retrieve a specific CSS value (like the color of an item), <strong>and</strong> to set one or more CSS properties on a set of<br />

elements. Secondly, methods like addClass, removeClass, toggleClass, <strong>and</strong> hasClass enable you to<br />

alter or <strong>in</strong>spect the CSS classes that are applied to elements. Furthermore, a couple of methods enable<br />

you to work with the dimensions <strong>and</strong> position of an element. I discuss only the most common ones, but<br />

you can look up the entire list at the jQuery website. The examples once aga<strong>in</strong> use the same HTML<br />

fragment you saw before, so it’s easy to follow along if you want to try them out <strong>in</strong> your browser.<br />

css(name, value)<br />

This method enables you to set a specific CSS property on a matched element. The name argument<br />

refers to a CSS property (such as border, color, <strong>and</strong> so on) <strong>and</strong> the value def<strong>in</strong>es the style you<br />

want to apply. The follow<strong>in</strong>g example changes the background color of the h1 element:<br />

css(name)<br />

$('h1').css('background-color', 'green');<br />

This method retrieves a specific CSS value based on the property you pass to it. The follow<strong>in</strong>g example<br />

alerts 'italic' because that’s the font-style of the element <strong>in</strong> the head<strong>in</strong>g level 2:<br />

alert($('h2 span').css('font-style'));<br />

You can use this value <strong>in</strong> your jQuery scripts; for example, you can use it to toggle the font-style<br />

between italic <strong>and</strong> normal or to set multiple elements to the same style.<br />

css(properties)<br />

This is quite a powerful method because it enables you to set multiple properties on the matched<br />

elements <strong>in</strong> one fell swoop. The follow<strong>in</strong>g example changes the color of all cells <strong>in</strong> the table to red,<br />

changes the font to Verdana <strong>and</strong> sets their padd<strong>in</strong>g to 10px. You pass the data <strong>in</strong> what is called an<br />

anonymous object where you wrap the entire set of properties <strong>in</strong> a pair of curly braces ({}), separate<br />

each property <strong>and</strong> value by a colon (:) <strong>and</strong> each pair by a comma:<br />

$('#DemoTable td').css({'color' : 'red', 'font-family' : 'Verdana',<br />

'padd<strong>in</strong>g' : '10px'});<br />

addClass, removeClass, <strong>and</strong> toggleClass<br />

The addClass <strong>and</strong> removeClass methods enable you to add <strong>and</strong> remove classes from elements,<br />

respectively. Just as with pla<strong>in</strong> CSS, you’re better off us<strong>in</strong>g these methods than assign<strong>in</strong>g <strong>in</strong>l<strong>in</strong>e CSS

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

Saved successfully!

Ooh no, something went wrong!