Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery :nth-last-child() Selector

jQuery :nth-last-child() Selector

jQuery selector jQuery selector

Example

Select the

element that is the third child of its parent, counting from the last child:

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

Try it »


Definition and Usage

:nth-last-child(n) selector selects all elements that are the n child of their parent, counting from the last child.

Tip:Please use the :nth-last-of-type() selector to select theregardless of typechild of n child of their parent, counting from the last child.


Syntax

:nth-last-child(

n

|even|odd|

formula

)

Parameters Description
n The index of each child element to be matched.
 必须是一个数字。第一个元素的索引号是 1。 |

| even | Selects every even child element. | | odd | Selects every odd child element. | | formula | Specifies which child elements to select via the (an + b) to select. Example: p:nth-last-child(3n+2) selects every third element, starting from the second-to-last child. |


Try it - Example

Select all

every element that is the first child of its parent

elements, counted starting from the last child
How to select all

the first child of its parent

element, counting from the last child.

Using the formula (an + b)
How to use the formula (an + b) to select different child elements, counting from the last child.

Using "even" and "odd"
How to use even and odd to select different child elements, counting from the last child.

Difference between :nth-child(), :nth-last-child(), :nth-of-type(), and :nth-last-of-type()
Difference between p:nth-child(2), p:nth-last-child(2), p:nth-of-type(2), and p:nth-last-of-type(2).


jQuery selector jQuery selector