【发布时间】:2012-12-25 10:40:03
【问题描述】:
我知道你可以用:last-child 选择最后一个孩子或用:nth-child 选择某个孩子(如果你知道他们的位置/编号)。
我需要的是在不知道可能有多少子元素的情况下选择最后 3 个子元素。
我知道有一个东西叫做:nth-last-child,但我真的不明白它是如何工作的。
为此:
<div id="something">
<a href="images/x.jpg" ><a/>
<a href="images/x.jpg" ><a/>
<!-- More elements here -->
<!-- ..... -->
<!-- I need to select these: -->
<a href="images/x.jpg" ><a/>
<a href="images/x.jpg" ><a/>
<a href="images/x.jpg" ><a/>
</div>
我需要这样的东西:
#something a:last-child{
/* only now it selects the last <a> and the 2 <a>'s that come before*/
}
【问题讨论】:
-
:nth-last-child,与:nth-child基本相同,但从最后一个到第一个afaik。 -
现在的问题是需要选择最后 3 个
-
P.S.结束标签是
</div>而不是<div/>。
标签: css css-selectors