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.

14<br />

CHAPTER 1 ■ INTRODUCING JQUERY<br />

To select descendant spans in your test document, execute the following comm<strong>and</strong> in the Firebug<br />

console:<br />

$("body span");<br />

This will find all spans contained within the body tag () of the document, even though the<br />

spans are also inside paragraph tags.<br />

>>> $("body span");<br />

[ span, span.foo ]<br />

Selecting Child Elements<br />

Child elements are a more-specific style of descendant selector. Only the very next level of element is<br />

considered for matching. To select a child element, use the parent element followed <strong>by</strong> a greater than (>)<br />

symbol, followed <strong>by</strong> the child element to match:<br />

parent>child<br />

In your test file, try to select any spans that are child elements of the body element <strong>by</strong> entering the<br />

following comm<strong>and</strong> in the console:<br />

$("body>span");<br />

Because there are no spans directly contained within the body element, the console will output the<br />

following:<br />

>>> $("body>span");<br />

[ ]<br />

Next, filter all span elements that are direct children of a paragraph element:<br />

$("p>span");<br />

The resulting output looks like this:<br />

>>> $("p>span");<br />

[ span, span.foo ]<br />

Selecting Next Elements<br />

Occasionally in a script, you’ll need to select the next element in the DOM. This is accomplished <strong>by</strong><br />

providing an identifier for the starting element (any selector pattern works here), followed <strong>by</strong> a plus sign<br />

(+), followed <strong>by</strong> the selector to match the next instance of:<br />

start+next<br />

Try this in the console <strong>by</strong> typing the following comm<strong>and</strong>:

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

Saved successfully!

Ooh no, something went wrong!