jQuery :nth-​​child()セレクター

❮jQueryセレクター

親の3番目の子である各<p>要素を選択します。

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

定義と使用法

:nth-​​child(n )セレクターは、タイプに関係なく、親のn番目の子であるすべての要素を選択します。

ヒント::nth-of-type()セレクターを使用して、親の特定のタイプのn番目の子であるすべての要素を選択します。


構文

:nth-child(n|even|odd|formula)

Parameter Description
n The index of each child to match.

Must be a number. The first element has the index number 1.
even Selects each even child element
odd Selects each odd child element
formula Specifies which child element(s) to be selected with a formula (an + b).
Example: p:nth-child(3n+2) selects each 3rd paragraph, starting at the 2nd child element

自分で試してみてください-例


の2番目の子である各<p>要素を選択する方法。

使用
さまざまな子要素を選択する方法。


の使用偶数と奇数を使用して異なる子要素を選択する方法。


の違いp:nth-​​child(2)、pの違い:nth-​​last-child(2)、p:nth-​​of-type(2)およびp:nth-​​of-last-type(2)。


❮jQueryセレクター