【问题标题】:Select specific element in HTML using CSS selectors [duplicate]使用 CSS 选择器选择 HTML 中的特定元素 [重复]
【发布时间】:2019-11-27 18:45:10
【问题描述】:

这是 HTML 结构。这种数据是动态生成的。它将有一些 <p> 标签,只有 c1 类,还有一些 <p> 标签,类 c1c2 两者都有。

<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 类的 &lt;p&gt; 标记并将样式应用到它。即在给定的示例代码中,我需要选择样式并将其应用到具有类 c1 gh&lt;p&gt; 标记。

我为此编写了以下 CSS 代码。它选择了类 c1 的所有&lt;p&gt;,但不知道如何选择此类事件的最后一个元素。

<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


【解决方案1】:

:last-of-type 选择最后一个匹配元素。 :not(.class) 只是选择后应用的条件。选择器创建一个重叠组:

  • c1:last-of-type = ["第 6 段。"]
  • :not(.c2) = ["第 1 段。","第 2 段。","第 3 段。","第 4 段。"]

这些集合不重叠,所以选择器不匹配任何元素。


不久前我试图找到一个类似的选择器。 CSS 3 选择器的开发可能允许这样做。它允许您这样做:

p:nth-last-child(1 of :not(.c2)) {
    background: red;
}

但在撰写本文时,browser support 很差。它唯一可以工作的浏览器是 Safari(macOS 和 iOS)9+。所以这个解决方案在大多数情况下仍然不可用。

或者,如果元素始终是第 4 个元素,您可以使用 :nth-of-type(4)。或者你可以使用:nth-last-of-type(3),如果元素总是在最后一个元素之前正好2个元素。

/* applicable only if the target element is always exactly 4th in .container */
.container .c1:nth-of-type(4) {
  background: red;
}


/* applicable only if the target element is always second to last */
.container .c1:nth-last-of-type(3) {
  background: red;
}


/* poor browser support (Safari 9+ at the time of writing) */
.container .c1:nth-last-child(1 of :not(.c2)) {
  background: blue;
}
<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>

如果这些都不适用,JavaScript 是一种解决方案,但在脚本被阻止的情况下它将不起作用。

当我想解决这个问题时,我选择编辑标记以添加标识我正在寻找的元素的类。

【讨论】:

  • 我认为这是因为你不访问最后一个 p 不是 c2 而是第 4 个总是
  • 也许吧。但是它解释了它适用于哪种情况,我不建议将其作为通用解决方案。可能是因为我不够清楚而误会了?
  • 我赞成,尽管基本上说“不,这是不可能的。如果它是一个动态创建的列表,由 javascript 制作,那么使用 javascript 将特定的类添加到要突出显示的元素”。
  • @RickardElimää 谢谢。今天确实如此,但是当复杂的选择器列表语法得到广泛支持时,这个答案将在未来出现。
【解决方案2】:

只有 CSS 是不可能的。最后一个类型的 Selector 调用实际上是最后一个 p-tag,如果它不与所选类一起使用,您的条件只会影响最后一个 p-tag。

我看到的唯一方法是,您必须使用您的框架或 JS 设置一个新类。

当您知道该元素是第 4 个时,您可以使用 :nth-child(4) 或者如果它是倒数第 3 个 :nth-last-child(3)

【讨论】:

    【解决方案3】:

    CSS 属性 nth-child

    .container p:not(.c2):nth-child(4){ background: red; }
    

    CSS 属性 nth-of-type

    .container p:not(.c2):nth-of-type(4) { background: red; }
    

    CSS 属性 nth-last-child

    .container p:not(.c2):nth-last-child(3){ background: red; }
    

    【讨论】:

    • 但是如果元素超过 6 个怎么办?那么你必须手动更改!有没有什么解决方案可以在没有 js 或 jquery 的情况下动态完成??
    • 是的,如果有 x 个元素,您必须更改 CSS。动态地从 CSS 中拒绝它是不可能的。我可以建议你在这里使用不同的方法添加任何唯一的类或元素来定位下一个兄弟元素。
    • 您也忘记添加.gh 作为答案。 :P
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多