html - CSS 选择器 : finding the last sibling of a type

标签 html css css-selectors

我有一个看起来像这样的文档:

<h1>Title A</h1>
<h2>Sub 1</h2>
<h1 class='foo'>Title B</h1>
<h2>Sub 2</h2>
<h2>Sub 3</h2>
<h1>C</h1>
<h2>Sub 4</h2>

我想选择 Sub 2 和 3,因为它们在 Title B 之后,是类 foo 的 h1。但是,我对 Sub 4 不感兴趣,因为前面的 h1 属于 foo 类。

我知道我可以在每个以 h1 开头的 block 周围放置 div,但我的内容是通过 markdown 生成的,所以唯一的方法是在页面加载后更改 DOM,这感觉又脏又麻烦。

我的问题与这个有点相关How to select all elements after specific element in CSS但是一旦看到常规的 h1,我就缺乏“恢复正常”的行为。

最佳答案

http://jsfiddle.net/70zxd3h8/

.foo + h2, .foo + h2 + h2 {
    background:red;
}

或者h2:nth-of-type(2), h2:nth-of-type(3)

关于html - CSS 选择器 : finding the last sibling of a type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27668017/

相关文章:

css - 第 nth-child 没有选择我图库中的特定图像

javascript - 在移动浏览器中播放声音?

javascript - 单击关闭后从 url 中删除#about

javascript - 按特定顺序遍历表头?

javascript - 如何在纯 javascript 中更改输入焦点上的标签颜色?

html - 我可以仅在与 div 关联的伪元素内使用 CSS 属性水平对齐两个 div 吗?

javascript - 在输入类型 ="number"的 Chrome 软键盘中显示 <Go> 而不是 <Next>

javascript 无法使用 onsen.ui

html - 有什么方法可以在每次包裹在 flexbox 后访问第一个元素?

css - 如何选择包含具有特定文本的列的行?