jQuery :nth-child()Selector

jQuery 选择器jQuery Selectors

Example

Select each <p> element that is the third child of its parent:

$("p:nth-child(3)")

Try it Yourself »

Definition and Usage

:nth-child(nregardless of type, thennth child element of its parent.

Tip:Use the:nth-of-type()selector to select all elements that are thespecific typeof thennth child of its parent.


Syntax

:nth-child(n|even|odd|formula)

Parameter Description
n The index of each child element to match.

Must be a number. The index of the first element is 1.
even Selects every even child element.
odd Selects every odd child element.
formula Specifies which child element is selected via the formula (an + b).
Example: p:nth-child(3n+2) selects every third paragraph, starting from the second child element.


实例

Try it Yourself - Examples

Select each <p> element that is the second child of all <div> elements
How to select each <p> element that is the second child of all <div> elements.

Using the formula (an + b)
How to use the formula (an + b) to select different child elements.

Using "even" and "odd"
How to use even and odd to select different child elements.

The difference between :nth-child(), :nth-last-child(), :nth-of-type() and :nth-of-last-type()
The difference between p:nth-child(2), p:nth-last-child(2), p:nth-of-type(2) and p:nth-last-of-type(2).


jQuery 选择器jQuery Selectors

Other Extensions