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) 之間的區別。