<div class="commentList">
<article class="comment " id="com21"></article>
<article class="comment " id="com20"></article>
<article class="comment " id="com19"></article>
<div class="something"> hello </div>
</div>
我想选择 #com19
?
.comment {
width:470px;
border-bottom:1px dotted #f0f0f0;
margin-bottom:10px;
}
.comment:last-child {
border-bottom:none;
margin-bottom:0;
}
只要我确实有另一个 div.something
作为commentList 中的实际最后一个孩子,这不起作用。在这种情况下是否可以使用 last-child 选择器来选择 article.comment
的最后一次出现?
原文由 matt 发布,翻译遵循 CC BY-SA 4.0 许可协议
:last-child
仅当相关元素是容器的最后一个子元素时才有效,而不是特定类型元素的最后一个子元素。为此,您需要:last-of-type
http://jsfiddle.net/C23g6/3/
根据@BoltClock 的评论,这仅检查最后一个
article
元素,而不是.comment
类的最后一个元素。