返回值:jQuery:nth-last-child(n|even|odd|formula)
V1.9概述
選擇所有他們父元素的第n個(gè)子元素。計(jì)數(shù)從最后一個(gè)元素開(kāi)始到第一個(gè)。
因?yàn)閖Query的實(shí)現(xiàn):nth-child(n)是嚴(yán)格來(lái)自CSS規(guī)范,所以n值是“1索引”,也就是說(shuō),從1開(kāi)始計(jì)數(shù)。對(duì)于所有其他選擇器表達(dá)式,jQuery遵循JavaScript的“0索引”的計(jì)數(shù)。因此,給定一個(gè)單一<ul>包含兩個(gè)<li>,?$('li:nth-child(1)')選擇第一個(gè)<li>,而$('li:eq(1)')選擇第二個(gè)。
這個(gè)不尋常的用法,可進(jìn)一步討論中找到W3C CSS specification.
參數(shù)
nV1.9
The index of each child to match.
Must be a number. The first element has the index number 1.
evenV1.9
Selects each even child element
oddV1.9
Selects each odd child element
formulaV1.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
示例
在每個(gè)匹配的ul中查找倒數(shù)第二個(gè)li
<ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> </ul> $("ul li:nth-last-child(2)");