Tải bản đầy đủ - 0 (trang)
Chapter 8. Selectors and Selection Methods

Chapter 8. Selectors and Selection Methods

Tải bản đầy đủ - 0trang

www.it-ebooks.info

pass to $(). Before explaining selector combinations and selector groups, we must explain the syntax of simple selectors.



Simple Selectors

A simple selector begins (explicitly or implicitly) with a tag type

specification. If you are only interested in

tags, for example,

your simple selector would begin with “p”. If you want to select

elements without regard to their tagname, use the wildcard “*”

instead. If a selector does not begin with either a tagname or a

wildcard, the wildcard is implicit.

The tagname or wildcard specifies an initial set of document

elements that are candidates for selection. The portion of the

simple selector that follows this type specification consists of

zero or more filters. The filters are applied left-to-right, in the

order that they appear, and each one narrows the set of selected

elements. Table 8-1 lists the filters supported by jQuery.

Table 8-1. jQuery Selector Filters

Filter



Meaning



#id



Matches the element with an id attribute of id. Valid HTML

documents never have more than one element with the same

id, so this filter is usually used as a standalone selector.



.class



Matches any elements whose class attribute (when interpreted as a list of words separated by spaces) includes the

word class.



[attr]



Matches any elements that have an attr attribute (regardless of its value).



[attr=val]



Matches any elements that have an attr attribute whose

value is val.



[attr!=val]



Matches elements that have no attr attribute, or whose

attr attribute is not equal to val (jQuery extension).



[attr^=val]



Matches elements whose attr attribute has a value that

begins with val.



[attr$=val]



Matches elements whose attr attribute has a value that

ends with val.



90 | Chapter 8: Selectors and Selection Methods



www.it-ebooks.info

Filter



Meaning



[attr*=val]



Matches elements whose attr attribute has a value that

contains val.



[attr~=val]



Matches elements whose attr attribute, when interpreted

as a list of words separated by spaces, includes the word

val. Thus the selector “div.note” is the same as

“div[class~=note]”.



[attr|=val]



Matches elements whose attr attribute has a value that

begins with val and is optionally followed by a hyphen and

any other character.



:animated



Matches elements that are currently being animated by

jQuery.



:button



Matches