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.

Only one paragraph in our document has an id of "bar", as we see in the result:<br />

>>> $("#bar");<br />

[ p#bar ]<br />

Combining Selectors for More-Precise Selection<br />

CHAPTER 1 ■ INTRODUCING JQUERY<br />

In some situations, it may be necessary to isolate only certain tags that correspond to a class, which is<br />

easy to do <strong>by</strong> combining tag type <strong>and</strong> class in your selector.<br />

Enter the following in the console to select only paragraph tags with the class foo:<br />

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

The results in the console confirm that the span was ignored, even though it has the class foo:<br />

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

[p.foo]<br />

Using Multiple Selectors<br />

In the event that you need to access multiple elements, multiple selectors can be used to access all of<br />

those elements at once. For instance, if you wanted to select any paragraph tag with a class of foo or any<br />

element with an ID of bar, you would use the following:<br />

$("p.foo,#bar");<br />

This returns elements that match at least one selector specified in |the string:<br />

>>> $("p.foo,#bar");<br />

[ p.foo, p#bar ]<br />

Hierarchy Selectors<br />

Sometimes, it’s not enough to be able to select <strong>by</strong> element, class, or ID. There are points at which you’ll<br />

need to access elements contained within, next to, or after another element, such as removing an active<br />

class from all menu items except the one that was just clicked, grabbing all the list items out of the<br />

selected unordered list, or changing attributes on the wrapper element when a form item is selected.<br />

Selecting Descendant Elements<br />

Selecting descendant elements, which are elements contained within other elements, is done using the<br />

ancestor selector followed <strong>by</strong> a space <strong>and</strong> the descendant selector.<br />

ancestor descendent<br />

13

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

Saved successfully!

Ooh no, something went wrong!