【发布时间】:2019-11-27 18:45:10
【问题描述】:
这是 HTML 结构。这种数据是动态生成的。它将有一些 <p> 标签,只有 c1 类,还有一些 <p> 标签,类 c1 和 c2 两者都有。
<div class="container">
<p class="c1 ab">The 1st paragraph.</p>
<p class="c1 cd">The 2d paragraph.</p>
<p class="c1 ef">The 3rd paragraph.</p>
<p class="c1 gh">The 4th paragraph.</p>
<p class="c1 ij c2">The 5th paragraph.</p>
<p class="c1 kl c2">The 6th paragraph.</p>
</div>
我需要选择最后一个带有 c1 类而不是 c2 类的 <p> 标记并将样式应用到它。即在给定的示例代码中,我需要选择样式并将其应用到具有类 c1 gh 的<p> 标记。
我为此编写了以下 CSS 代码。它选择了类 c1 的所有<p>,但不知道如何选择此类事件的最后一个元素。
<style>
.container p:not(.c2) { background: red; }
</style>
我为此使用了以下代码,但它不起作用。
.container p:not(.c2):last-of-type { background: red; }
.container p:not(.c2) :last-of-type { background: red; }
提前感谢您的帮助!
【问题讨论】:
-
参考这里以获得正确的选择器stackoverflow.com/questions/4500572/…
-
@PrabhjotSinghKainth 这仅适用于固定的 DOM 结构。
-
你想要最后2个背景颜色红色..请解释更多
-
@Ranjithv 我想这只是一个MRE。
-
@roy - 怎么样?
.container p:nth-last-child(1 of :not(.c2)) { background: red; }不这样做
标签: html css css-selectors