CSS最后一个子选择器:选择最后一个元素的特定类,而不是最后一个子? [英] CSS last-child selector: select last-element of specific class, not last child inside of parent?

查看:1106
本文介绍了CSS最后一个子选择器:选择最后一个元素的特定类,而不是最后一个子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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中的实际最后一个孩子。在这种情况下是否可以使用最后一个子选择器来选择 article.comment

That does not work as long as I do have another div.something as actual last child in the commentList. Is it possible to use the last-child selector in this case to select the last appearance of article.comment?

以下是一个示例: http://jsfiddle.net/C23g6/2/

谢谢

推荐答案

:last-child 只有当有问题的元素是容器的最后一个子元素,而不是特定类型的元素的最后一个元素时才有效。为此,您需要 :最后类型

:last-child only works when the element in question is the last child of the container, not the last of a specific type of element. For that, you want :last-of-type

http://jsfiddle.net/C23g6/3 /

根据@ BoltClock的评论,这只是检查最后的文章具有 .comment 类的最后一个元素。

As per @BoltClock's comment, this is only checking for the last article element, not the last element with the class of .comment.

这篇关于CSS最后一个子选择器:选择最后一个元素的特定类,而不是最后一个子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆