css - 是否为 :not() negation accept descendant selectors?

标签 css css-selectors

<分区>

我一直在使用 :not() 伪类来设置样式,而不需要用第二个不必要的声明覆盖它来撤消第一个, 但现在我遇到了一个奇怪的行为,即 Safari 接受 :not() 中的后代选择器,但 Chrome 不接受。

我使用了类似a:not(.blue a)的东西。

我搜索了答案,但我仍然没有完全理解原因。 规范真的允许后代选择器吗?

这是一个演示:

a:not(.blue a) {
  color: red;
}
<div><a>this one should be in red</a></div>
<div class="blue"><a>this one shouldn't</a></div>

http://codepen.io/oscarmarcelo/pen/YqboQJ?editors=1100

最佳答案

Selectors Level 3 ,答案是否定的。 :not() 表示法只接受简单的选择器

6.6.7. The negation pseudo-class

The negation pseudo-class, :not(X), is a functional notation taking a simple selector (excluding the negation pseudo-class itself) as an argument. It represents an element that is not represented by its argument.

什么是简单选择器?

来自 selector syntax :

A simple selector is either a type selector, universal selector, attribute selector, class selector, ID selector, or pseudo-class.

与后代选择器无关。

然而,在Selectors Level 4 , :not() 接受复杂选择器,其中包括后代组合器。浏览器对该规范的支持仍然很弱。

关于css - 是否为 :not() negation accept descendant selectors?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37305923/

相关文章:

css - 不在其他元素内部的元素

javascript - jQuery 在使用 rowspan 时选择列中的最后一个单元格

python - 如何避免在网络爬行时出现断词

java - 基于内部子属性的 webDriver cssSelector

html - 如何避免固定页脚重叠?

javascript - 通过降低高度来验证单独的文本字段样式不起作用

javascript - 如何在表格行之间显示元素?

html - 为什么页脚上方的边距比其余部分小?

css - 将文本输入和按钮全宽组合在响应式布局中?

html - 使用单页导航突出显示事件选项卡