css - 当元素类型不同时禁用最后一个元素的边框

标签 css css-selectors

当元素类型不同时,如何禁用最后一个元素的边框,即。一个是段落,另一个是超链接?

http://jsfiddle.net/e4zjcmue/

div *:last-of-type:after { border-right: none; 不幸的是禁用了所有边框。

div * {
  display: inline;
}

div *:after {
  content: '';
  border-right: 1px solid blue;
}

div *:last-of-type:after {
  /* border-right: none; */
}
<div>
    <p>Lorem</p>
    <a href="#">Ipsum</a>
</div>

最佳答案

您似乎要寻找的选择器是 :last-child 而不是 last-of-type

*:last-of-type 选择器选择父级下每个单一类型的最后一个元素。也就是说,它将选择最后一个 div、最后一个 p 等。在您的代码段中,第一个元素也是其类型的最后一个元素,因此所有边框都消失了.

然而,:last-child 仅选择父元素的最后一个子元素,因此在此实例中仅从 a 标签中移除边框。 :last-child 之前的 * 不是强制性的,因为选择器默认选择最后一个子元素,而不管其类型。

div * {
  display: inline;
}
div *:after {
  content: '';
  border-right: 1px solid blue;
}
div :last-child:after {
  border-right: none;
}
<div>
  <p>Lorem</p>
  <a href="#">Ipsum</a>
</div>

关于css - 当元素类型不同时禁用最后一个元素的边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32550066/

相关文章:

python - 如何使用 Selenium 将鼠标悬停在多个元素上并通过 Python 提取文本

javascript - 在模式中单击单元格或作为弹出或警报对话框时显示表格单元格详细信息

asp.net-mvc - 我如何在 mvc actionlink 语法中添加 Bootstrap 图像?

jquery - 带有 CSS 或 jQuery 的非常宽的薄花括号

css - 嵌套主题组件 : apply styles depending on a theme class of the nearest themed parent, 忽略承载主题类的更深层次父级

selenium - 需要通过 css 在 selenium 中查找元素

css - 从数据 URI 确定图像 URL

javascript - 将效果淡入 Bootstrap 3 轮播

xml - Nokogiri 的 clojure 等价物是什么(用于使用 xpath 和 css 选择器解析 xml)

python - 在scrapy中提取类名