Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery :nth-child() Selector

jQuery :nth-child() Selector

jQuery selector jQuery selector

Example

Select every element that is the third child of its parent

element:

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

Try it »


Definition and Usage

:nth-child(n) selector selects all elements that are the n child of their parent, regardless of type.

Tip:Please use the :nth-of-type() selector to select thespecific typechild of n child of their parent, regardless of type.


Syntax

:nth-child(

n

|even|odd|

formula

)

Parameters Description
n The index of each child element to be matched.
 必须是一个数字。第一个元素的索引号是 1。 |

| even | Selects every even child element. | | odd | Selects every odd child element. | | formula | Specifies which child elements to select via the (an + b).
Example: p:nth-child(3n+2) selects every third element, starting from the second child. |


Try it - Example

Select all

every element that is the second child of its parent

Element
How to select all

every element that is the second child of its parent

.

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.

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


jQuery selector jQuery selector