Web Development with jQuery®

(Elliott) #1

Filtering Selections and Arrays (^) ❘ 145
The following script demonstrates how the filter() method uses a selector to indicate which items
should be in the selection:
$(document).ready(
function()
{
$('ul#rubberSoul li')
.filter('li.George')
.addClass('rubberSoulGeorge')
.end()
.filter('li.John')
.addClass('rubberSoulJohn')
.end()
.filter('li.Paul')
.addClass('rubberSoulPaul')
.end();
}
);
In the preceding script, the selector li.George reduces the selection to include only the


  • elements that have a class name of George; then the class name rubberSoulGeorge is added to each of
    those
  • elements, and the same happens for Paul and John. Just before the time a new filter()
    is attempted, a call to end() removes the last fi lter applied to the selection. Figure 5-4 shows this
    example in Safari.


    FIGURE 5-4


    http://www.it-ebooks.info

  • Free download pdf