【问题标题】:CSS3 selector for "not A and not B"?“不是 A 而不是 B”的 CSS3 选择器?
【发布时间】:2012-09-05 16:54:14
【问题描述】:

我有这段代码,它使每个段落的类都不同于“cl2”红色。

<head>
<style type="text/css">
p{ color:#000000; }
:not(.cl2){ color:#ff0000; }
</style>
</head>
<body>
<p class="cl1">This is a paragraph.</p>
<p class="cl2">This is second paragraph.</p>
<p class="cl2">This is third paragraph.</p>
<p class="cl3">This is fourth paragraph.</p>
<p class="cl2">This is fifth paragraph.</p>
<p class="cl2">This is sixth paragraph.</p>
<p class="cl4">This is seventh paragraph.</p>
<p class="cl5">This is eigth paragraph.</p>
<p class="cl1">This is nineth paragraph.</p>
</body>

如何扩展我的:not 选择器以忽略例如类“cl2”和“cl4”?我试过了::not(.cl2, .cl4){ color:#ff0000; } 但它不起作用。

【问题讨论】:

  • CSS 仅支持通过XY 的连词,其中XY 表示选择器(例如.cl2.cl4),AFAIK。是否可以在 CSS3 中做:not(.cl2):not(.cl4)
  • @pst: Yes. (see also)

标签: css css-selectors boolean-logic


【解决方案1】:
:not(.cl2):not(.cl4){ color:#ff0000; }

http://jsfiddle.net/nottrobin/WFwtP/

注意有 differences between the :not selector in CSS3 vs jQuery - 由于在 CSS4 规范中收敛(感谢 @BoltClock)

【讨论】:

  • 之所以必须这样做是因为:not(.cl2, .cl4) 在 CSS3 中无效,但在 jQuery 中有效并建议用于 CSS4。见this question
  • 谢谢 我不知道它是有效的 jQuery 语法,我什至不知道 CSS4 是什么东西。太棒了。
  • 显然是CSS4 is not a thing(那是我的错误)。我真的指的是 Selectors 4 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-15
  • 2012-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-29
  • 1970-01-01
相关资源
最近更新 更多