如何选择具有特定类的最后一个元素,而不是父级中的最后一个子元素?
<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
的最后一次出现?
<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>
I want to select #com19
?
.comment {
width:470px;
border-bottom:1px dotted #f0f0f0;
margin-bottom:10px;
}
.comment:last-child {
border-bottom:none;
margin-bottom:0;
}
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
:last-child
仅当相关元素是容器的最后一个子元素而不是特定类型元素的最后一个元素时才起作用。为此,您需要:last-of -类型
http://jsfiddle.net/C23g6/3/
根据@BoltClock 的comment,这仅检查最后一个
article
元素,而不是最后一个具有.comment
类的元素。: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/
As per @BoltClock's comment, this is only checking for the last
article
element, not the last element with the class of.comment
.现在可以通过仔细使用
:has()
来解决这个问题,具体来说:类似的技术还允许您选择除容器中最后一次出现的类之外的任何内容,或一组元素中的最后一次出现。有关示例,请参阅下面的代码片段。
注意:
has()
目前在 Chrome、Edge、Safari 和 Safari 中受支持。 Firefox 至少自 2023 年 12 月起2024 年 5 月注意事项:自 2023 年 3 月起,现在还提供了对
:nth-child(n of)
和:nth-last-child 的基线支持(<选择器> 的 n)
。请参阅 MDN 文档。根据您的浏览器支持配置文件 - 权衡常绿与较旧的 safari 使用情况 - 此语法更清晰(专门用于此目的)并且可能得到更好的支持:
This can now be solved with careful use of
:has()
, specifically:A similar technique also allows you to select anything but the last occurrence of a class in a container, or the last occurrence within a group of elements. See the snippet below for examples.
Note:
has()
is currently supported in Chrome, Edge, Safari & Firefox since at least Dec 2023Note May 2024: Since March 2023 there is now also baseline support for
:nth-child(n of <selector>)
and:nth-last-child(n of <selector>)
. See the MDN docs.Depending on your browser support profile - weighing up evergreen vs. older safari usage - this syntax is both clearer (it's intended for this purpose specifically) and potentially better supported:
我猜最正确的答案是:使用
:nth-child
(或者,在这种特定情况下,使用其对应的:nth-last-child
)。大多数人只知道该选择器的第一个参数是基于 n 的计算来获取一系列项目,但它也可以采用“[任何 CSS 选择器]”的第二个参数。您的场景可以使用此选择器来解决:
.commentList .comment:nth-last-child(1 of .comment)
但技术上正确并不意味着您可以使用它,因为此选择器目前仅在 Safari 中实现。
进一步阅读:
I guess that the most correct answer is: Use
:nth-child
(or, in this specific case, its counterpart:nth-last-child
). Most only know this selector by its first argument to grab a range of items based on a calculation with n, but it can also take a second argument "of [any CSS selector]".Your scenario could be solved with this selector:
.commentList .comment:nth-last-child(1 of .comment)
But being technically correct doesn't mean you can use it, though, because this selector is as of now only implemented in Safari.
For further reading:
这在所有主流浏览器中都可以正常工作;
This works fine in all major browsers;
新的
:has()
伪类(尚未被所有浏览器支持)让您非常接近解决方案:限制是这将找到任何带有
.class
后面跟着一个没有此类的元素。但这与问题的用例相匹配。The new
:has()
pseudo class (not yet supported by all browsers) lets you get pretty close to a solution:The limitation is that this will find any element with
.class
which is followed by an element that doesn't have this class. But this would match the use case of the question.如果要浮动元素,则可以反转顺序
,即
float: right;
而不是float: left;
然后使用此方法选择类的第一个子级。
这实际上只是将类应用于最后一个实例,因为它现在的顺序相反。
这是一个适合您的工作示例:
If you are floating the elements you can reverse the order
i.e.
float: right;
instead offloat: left;
And then use this method to select the first-child of a class.
This is actually applying the class to the LAST instance only because it's now in reversed order.
Here is a working example for you:
我认为应该在这里评论对我有用的东西:
在需要的地方多次使用
:last-child
,以便它始终获得最后一个。以此为例:
Something that I think should be commented here that worked for me:
Use
:last-child
multiple times in the places needed so that it always gets the last of the last.Take this for example:
这个解决方案怎么样?
What about this solution?