jQuery :nth-of-type() 选择器
定义和用法
The :nth-of-type(n) selector selects all elements that are the nth child, of a particular type, of their parent。
提示:使用 :nth-child() 选择器选择其父元素的第 n 个子元素,无论类型如何。
语法
:nth-of-type(n|even|odd|formula)
参数 | 描述 |
---|---|
n | 要匹配的每个子元素的索引。 必须是一个数字。第一个元素的索引号为 1。 |
even | 选择每个偶数子元素 |
odd | 选择每个奇数子元素 |
formula | 使用公式 (an + b) 指定要选择的子元素。 示例:p:nth-of-type(3n+2) 选择每隔 3 个段落,从第 2 个段落开始 |
自行尝试 - 示例
选择所有 <div> 元素中的第二个 <p> 元素
如何选择所有 <div> 元素中的第二个 <p> 元素。
使用公式 (an + b)
如何使用公式 (an + b) 选择不同的子元素。
使用 "even" 和 "odd"
如何使用 even 和 odd 来选择不同的子元素。
p:nth-child(), p:nth-last-child(), p:nth-of-type() 和 p:nth-of-last-type() 之间的区别
p:nth-child(2), p:nth-last-child(2), p:nth-of-type(2) 和 p:nth-of-last-type(2) 之间的区别。