Chapter 8. Selectors and Selection Methods
Weâve been using the jQuery selection function, $()
, with simple CSS selectors. It is now time
to study the jQuery selector grammar in depth, along with a number of
methods for refining and augmenting the set of selected
elements.
jQuery Selectors
jQuery supports a fairly complete subset of the selector grammar defined by the CSS3 Selectors draft standard, with the addition of some nonstandard but very useful pseudoclasses.
The selector grammar has three layers. Youâve undoubtedly seen the
simplest kind of selectors before. â#testâ selects an element with an
id
attribute of âtestâ, âblockquoteâ
selects all <blockquote>
tags
in the document, and âdiv.noteâ selects all
<div>
tags with a
class
attribute of ânoteâ. Simple
selectors can be combined into âselector combinationsâ such as âdiv.note>pâ and âblockquote iâ by
separating them with a combinator character. And
simple selectors and selector combinations can be grouped into
comma-separated lists. These selector groups are the most general kind
of selector that we 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 <p>
tags, for example, your simple selector would begin with âpâ. If you want to select elements without regard to their tagname, use the wildcard ...
Get jQuery Pocket Reference now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.