:nth-child(n)

本页内容
上一节: Sel_lastoftype 下一节: Sel_nthlastchild

jQuery :nth-child() 选择器

jQuery 选择器

选取属于其父元素的第三个子元素的每个 <p> 元素:


示例

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

定义和用法

nth-child(n) 选择器选取属于其父元素的不限类型的第 n 个子元素的所有元素。

提示:请使用 :nth-of-type() 选择器来选取属于其父元素的特定类型的第 n 个子元素的所有元素。

语法

    :nth-child(n|even|odd|formula)
参数 描述
n 要匹配的每个子元素的索引。必须是一个数字。第一个元素的索引号是 1。
even 选取每个偶数子元素。
odd 选取每个奇数子元素。
formula 规定哪个子元素需通过公式 (an + b) 来选取。 实例:p:nth-child(3n+2) 选取每个第三段,从第二个子元素开始。

尝试一下 - 实例

选取所有 <div> 元素的第二个子元素的每个 <p> 元素 如何选取所有 <div> 元素的第二个子元素的每个 <p> 元素。

使用公式 (an + b) 如何使用公式 (an + b) 来选取不同的子元素。

使用"偶数"和"奇数" 如何使用偶数和奇数来选取不同的子元素。

:nth-child()、:nth-last-child()、:nth-of-type() 和 :nth-of-last-type() 之间的不同 p:nth-child(2)、p:nth-last-child(2)、p:nth-of-type(2) 和 p:nth-last-of-type(2) 之间的不同。

jQuery 选择器

上一节: Sel_lastoftype 下一节: Sel_nthlastchild
此页面最后编辑于2022年8月19日 (星期五) 11:37。