jQuery :nth-last-child()セレクター
例
最後の子から数えて、親の3番目の子である各<p>要素を選択します。
$("p:nth-last-child(3)")
定義と使用法
:nth-last-child(n )セレクターは、タイプに関係なく、最後の子から数えて、親のn番目の子であるすべての要素を選択します。
ヒント::nth-last-of-type()セレクターを使用して、最後の子から数えて、親の特定のタイプのn番目の子であるすべての要素を選択します。
構文
:nth-last-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-last-child(3n+2) selects each 3rd paragraph, starting at the last 2nd child |
自分で試してみてください-例
を選択します。最後の子から数えて、すべての<div>要素の最初の子である<p>要素を選択する方法。
を使用し
て、最後の子から数えてさまざまな子要素を選択する方法。
の使用最後の子から数えて、偶数と奇数を使用して異なる子要素を選択する方法。
の違いp:nth-child(2)、pの違い:nth-last-child(2)、p:nth-of-type(2)およびp:nth-of-last-type(2)。