English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
$(:nth-child(number|An+(B|odd|even))
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 | Description |
---|---|
number | The index of each child to match (starting from1Start) |
odd | Select every odd child element |
even | Select every even child element |
An+B | Specify the child element to be selected Example: p:nth-child(3n + 2Select every third child starting from the second child |