返回值:jQuery :nth-last-child(n|even|odd|formula)

V1.9 概述

选择所有他们父元素的第n个子元素。计数从最后一个元素开始到第一个。

因为jQuery的实现:nth-child(n)是严格来自CSS规范,所以n值是“1索引”,也就是说,从1开始计数。对于所有其他选择器表达式,jQuery遵循JavaScript的“0索引”的计数。因此,给定一个单一<ul>包含两个<li>, $('li:nth-child(1)')选择第一个<li>,而$('li:eq(1)')选择第二个。

这个不寻常的用法,可进一步讨论中找到 W3C CSS specification .

参数

n V1.9

The index of each child to match.

Must be a number. The first element has the index number 1.

even V1.9

Selects each even child element

odd V1.9

Selects each odd child element

formula V1.9

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

示例

在每个匹配的ul中查找倒数第二个li

<ul>
	<li>1</li>
	<li>2</li>
	<li>3</li>
	<li>4</li>
</ul>

$("ul li:nth-last-child(2)");