选择同属于一个父元素之下,并且标签名相同的子元素中的第n个。
因为jQuery的实现:nth-是严格来自CSS规范,n值是“1-indexed”,也就是说,从1开始计数。对于所有其他选择器表达式比如:eq() 或 :even ,jQuery遵循JavaScript的“0索引”的计数。
这个不寻常的用法,可进一步讨论中找到W3C CSS specification.
The index of each child to match.
Must be a number. The first element has the index number 1.
Selects each even child element
Selects each odd child element
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
查找每个span,这个 span 是 其所有兄弟span元素中的第二个元素。
<div> <span>John</span> <b>Kim</b> <span>Adam</span> <b>Rafael</b> <span>Oleg</span> </div> <div> <b>Dave</b> <span>Ann</span> </div> <div> <i><span>Maurice</span></i> <span>Richard</span> <span>Ralph</span> <span>Jason</span> </div>
$("span:nth-of-type(2)");