Skip to content

Selector expressions in jQuery

注:本文内容摘录自《Learning jQuery (4th Edition)》。

The jQuery factory function $() is used to find elements on the page to work with. This function takes a string composed of CSS-like syntax, called a selector expression.

Simple CSS

SelectorMatches
*All elements.
#idThe element with the given ID.
elementAll elements of the given type.
.classAll elements with the given class.
a, bElements that are matched by a or b.
a bElements b that are descendants of a.
a > bElements b that are children of a.
a + bElements b that immediately follow a.
a ~ bElements b that are siblings of a and follow a.

Position among siblings

SelectorMatches
:nth-child(index)Elements that are the index child of their parent element (1-based).
:nth-child(even)Elements that are an even child of their parent element (1-based).
:nth-child(odd)Elements which are an odd child of their parent element (1-based).
:nth-child(formula)Elements that are the nth child of their parent element (1-based). Formulas are of the form an+b for integers a and b.
:nth-last-child()The same as :nth-child(), but counting from the last element to the first.
:first-childElements that are the first child of their parent.
:last-childElements that are the last child of their parent.
:only-childElements that are the only child of their parent.
:nth-of-type()The same as :nth-child(), but only counting elements of the same element name.
:nth-last-of-type()The same as :nth-last-child(), but only counting elements of the same element name.
:first-of-typeElements which are the first child of the same element name among their siblings.
:last-of-typeElements which are the last child of the same element name among their siblings.
:only-of-typeElements that are the only child of the same element name among their siblings.

Position among matched elements

SelectorMatches
:firstThe first element in the result set.
:lastThe last element in the result set.
:not(a)All elements in the result set that are not matched by a.
:evenEven elements in the result set (0-based).
:oddOdd elements in the result set (0-based).
:eq(index)A numbered element in the result set (0-based).
:gt(index)All elements in the result set after (greater than) the given index (0-based).
:lt(index)All elements in the result set before (less than) the given index (0-based).

Attributes

SelectorMatches
[attr]Elements that have the attribute attr.
[attr="value"]Elements whose attr attribute is value.
[attr!="value"]Elements whose attr attribute is not value.
[attr^="value"]Elements whose attr attribute begins with value.
[attr$="value"]Elements whose attr attributes ends with value.
[attr*="value"]Elements whose attr attribute contains the substring value.
[attr~="value"]Elements whose attr attribute is a space-delimited set of strings, one of which is value.
[attr|="value"]Elements whose attr attributes is either equal to value or begins with value followed by a hyphen.

Forms

SelectorMatches
:inputAll <input>, <select>, <textarea>, and <button> elements.
:textThe <input> elements with type="text".
:passwordThe <input> elements with type="password".
:fileThe <input> elements with type="file".
:radioThe <input> elements with type="radio".
:checkboxThe <input> elements with type="checkbox".
:submitThe <input> elements with type="submit".
:imageThe <input> elements with type="image".
:resetThe <input> elements with type="reset".
:buttonThe <input> elements with type="button" and <button> elements.
:enabledEnabled form elements.
:disabledDisabled form elements.
:checkedChecked checkboxes and radio buttons.
:selectedSelected <option> elements.

Miscellaneous selectors

SelectorMatches
:rootThe root element of the document.
:headerHeader elements (for example, <h1>, <h2>).
:animatedElements with an animation in progress.
:contains(text)Elements containing the given text.
:emptyElements with no child nodes.
:has(a)Elements containing a descendant element matching a.
:parentElements that have child nodes.
:hiddenElements that are hidden, either through CSS or because they are .
:visibleThe inverse of :hidden.
:focusThe elements that has the keyboard focus.
:lang(language)Elements with the given language code (either due to a lang attribute on the element or an ancestor, or a <meta> declaration).
:targetWhichever element is targeted by the URI's fragment identifier, if any.

天上的神明与星辰,人间的艺术和真纯,我们所敬畏和热爱的,莫过于此。