English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery :nth-child() selector

jQuery Selectors

nth-child(nSelector selects all elements belonging to the parent element ofThe nth childelements of the element.

Usingnth-of-type()The selector selects all elements of a specific type that belong to its parent element:The nth childAll elements of the element.

Syntax:

$(:nth-child(number|An+(B|odd|even))

Example

Each <p> element selected as the third child of its parent:

$("document").ready(function(){
  -child(3")
});
Test and see‹/›

Odd and even keywords are used to match child elements whose index is odd or even:

$("document").ready(function(){
  -
  $("li:nth-child(odd)
});
Test and see‹/›

Explain: using the formula (An + B)Arepresents a loop size,nis a counter (starting from 0), andBis an offset value:

$("document").ready(function(){
  -child(4n + 1")
  $("li:nth-child(3n()).css("background", "coral");
});
Test and see‹/›

Parameter Value

ParameterDescription
numberThe index of each child to match (starting from1Start)
oddSelect every odd child element
evenSelect every even child element
An+BSpecify the child element to be selected
Example: p:nth-child(3n + 2Select every third child starting from the second child

jQuery Selectors