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.

402 x CHAPTER 11 JQUERY<br />

In the follow<strong>in</strong>g table you see a list of jQuery’s basic filters. Remember, you can try out each example<br />

by replac<strong>in</strong>g the //Examples go here l<strong>in</strong>e with the code examples given. Then save the page<br />

<strong>and</strong> load it <strong>in</strong> your browser to see the code at work.<br />

FILTER<br />

:first<br />

:last<br />

:odd<br />

:even<br />

:eq(<strong>in</strong>dex)<br />

:lt(<strong>in</strong>dex)<br />

:gt(<strong>in</strong>dex)<br />

PURPOSE<br />

Enables you to select the first or last item <strong>in</strong> a matched set. The follow<strong>in</strong>g example<br />

changes the background color of the first or last row of the table to red:<br />

$('#DemoTable tr:first').css('background-color', 'red');<br />

$('#DemoTable tr:last').css('background-color', 'red');<br />

First, the table is found us<strong>in</strong>g #DemoTable. Then all its rows are found us<strong>in</strong>g tr.<br />

F<strong>in</strong>ally, the first or last row is found us<strong>in</strong>g the :first <strong>and</strong> :last filters.<br />

Enables you to select the odd or even items <strong>in</strong> a matched set. The follow<strong>in</strong>g<br />

example changes the background color of the odd rows of the table to red.<br />

Because the number<strong>in</strong>g is zero-based, you actually see the second <strong>and</strong> fourth<br />

rows change color (because they have an <strong>in</strong>dex of 1 <strong>and</strong> 3, respectively).<br />

$('#DemoTable tr:odd').css('background-color', 'red');<br />

Matches elements by their <strong>in</strong>dex. :eq (equals) returns a s<strong>in</strong>gle element by its<br />

<strong>in</strong>dex, <strong>and</strong> :lt (less than) <strong>and</strong> :gt (greater than) return items smaller or greater<br />

than the given <strong>in</strong>dex, respectively. Examples:<br />

// Changes the color <strong>in</strong> the first row (with an <strong>in</strong>dex of 0)<br />

$('#DemoTable tr:eq(0)').css('color', 'green');<br />

// Changes only the last two rows. The first three<br />

// with an <strong>in</strong>dex of 0, 1 <strong>and</strong> 2 respectively, are skipped.<br />

$('#DemoTable tr:gt(2)').css('color', 'green');<br />

// Changes the text color of the first two rows to green.<br />

$('#DemoTable tr:lt(2)').css('color', 'green');<br />

:header<br />

F<strong>in</strong>ds all headers (from h1 to h6) <strong>in</strong> the page. Example:<br />

$(':header').css('color', 'green');<br />

For a complete list of all basic filters, check out the jQuery documentation at http://api.jquery<br />

.com/category/selectors/.<br />

Advanced Filters<br />

Besides the basic filters you just saw, jQuery supports a lot more filters that enable you to get items<br />

based on the text they conta<strong>in</strong>, whether or not they are visible, <strong>and</strong> also on any attributes they may<br />

have. Additionally, you can f<strong>in</strong>d filters to get at form elements (like buttons, check boxes, radio buttons,<br />

<strong>and</strong> so on) <strong>and</strong> a number of selectors that enable you to select children, parents, sibl<strong>in</strong>gs, <strong>and</strong><br />

descendants. The follow<strong>in</strong>g table lists the ones you’ll use most. The onl<strong>in</strong>e jQuery documentation<br />

gives you access to the complete list, with full work<strong>in</strong>g examples show<strong>in</strong>g how they work.

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

Saved successfully!

Ooh no, something went wrong!