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.

$("p:parent");<br />

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

Visibility Filters<br />

CHAPTER 1 ■ INTRODUCING JQUERY<br />

Visibility filters, :hidden <strong>and</strong> :visible, will select elements that are, respectively, hidden <strong>and</strong> visible.<br />

Select all visible paragraphs like so:<br />

$("p:visible");<br />

Because none of the elements in your HTML example are currently hidden, this returns the<br />

following result set:<br />

>>> $("p:visible");<br />

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

Attribute Filters<br />

Element attributes are also a great way to select elements. An attribute is anything in the element that<br />

further defines it (this includes the class, href, ID, or title attributes). For the following examples, you'll<br />

be accessing the class attribute.<br />

■ Note Please bear in mind that it is faster (<strong>and</strong> better practice) to use ID (#id) <strong>and</strong> class (.class) selectors in<br />

production scripts whenever possible; the examples below are just to demonstrate the capabilities of the filter.<br />

Selecting Elements That Match an Attribute <strong>and</strong> Value<br />

To match elements that have a given attribute <strong>and</strong> value, enclose the attribute-value pair in square<br />

brackets ([]):<br />

[attribute=value]<br />

To select all elements with a class attribute of foo, execute the following in the console:<br />

$("[class=foo]");<br />

This returns the following:<br />

>>> $("[class=foo]");<br />

[ p.foo, span.foo ]<br />

19

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

Saved successfully!

Ooh no, something went wrong!