Does :not() negation accept descendant selectors? [duplicate]

无人久伴 提交于 2019-12-17 14:57:34

问题


I've been using the :not() pseudo-class to style things without the need to override it with a second unnecessary declaration to undo the first one, but now I came across a weird behaviour where Safari accepts descendant selectors within the :not(), but Chrome doesn't.

I used something like a:not(.blue a).

I searched for answers, but I still don't fully understand the reason.
Are descendant selectors really allowed by the spec?

Here's a demo:

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


回答1:


In Selectors Level 3, the answer would be NO. The :not() notation accepts only simple selectors.

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.

What is a simple selector?

From selector syntax:

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

Nothing about a descendant selector.

HOWEVER, in Selectors Level 4, :not() accepts complex selectors, which would include descendant combinators. Browser support is still quite weak for this specification.



来源:https://stackoverflow.com/questions/37305923/does-not-negation-accept-descendant-selectors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!