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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Additionally, like with .eq(), a negative index can be used. This can be applied to the start <strong>and</strong>/or<br />

end point.<br />

To select all paragraphs <strong>and</strong> then limit the selection to the second <strong>and</strong> third paragraphs, use the<br />

following code:<br />

$("p").slice(1,3);<br />

The result in the console reads as follows:<br />

>>> $("p").slice(1,3);<br />

[ p.foo, p ]<br />

To select the last two elements from the paragraph set, you would use the following:<br />

$("p").slice(-2);<br />

This generates the following result:<br />

>>> $("p").slice(-2);<br />

[ p, p#bar ]<br />

.children()<br />

Oftentimes, it becomes necessary to drill down in a result set to find child elements. This is<br />

accomplished using the .children() method, which accepts one optional parameter: a selector to match<br />

child elements against.<br />

To select all paragraphs <strong>and</strong> then change the selection to match all child elements of the<br />

paragraphs, execute the following code:<br />

$("p").children();<br />

This outputs the following:<br />

>>> $("p").children();<br />

[ span, span.foo ]<br />

If you need a more specific set of children than that, you’re able to pass an optional selector to the<br />

.children() method. To select all paragraphs <strong>and</strong> then find all children with a class foo, use the<br />

following:<br />

$("p").children(".foo");<br />

The results in the console are as follows:<br />

>>> $("p").children(".foo");<br />

29

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

Saved successfully!

Ooh no, something went wrong!