CSS last-child selector: select last-element of specific class, not last child inside of parent?(CSS last-child 选择器:选择特定类的最后一个元素,而不是父级中的最后一个子级?)
问题描述
<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
的最后一次出现?
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
?
jsFiddle
推荐答案
:last-child
仅在相关元素是容器的最后一个子元素而不是特定类型的最后一个子元素时有效的元素.为此,您需要 :last-of-type
: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 的评论,这只是检查最后一个 article
元素,而不是 .comment
类的最后一个元素.
As per @BoltClock's comment, this is only checking for the last article
element, not the last element with the class of .comment
.
body {
background: black;
}
.comment {
width: 470px;
border-bottom: 1px dotted #f0f0f0;
margin-bottom: 10px;
}
.comment:last-of-type {
border-bottom: none;
margin-bottom: 0;
}
<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>
这篇关于CSS last-child 选择器:选择特定类的最后一个元素,而不是父级中的最后一个子级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:CSS last-child 选择器:选择特定类的最后一个元素,


- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Fetch API 如何获取响应体? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01