这让我疯狂:
HTML:
<div><h1>Hello World!</h1></div>
Run Code Online (Sandbox Code Playgroud)
CSS:
*:not(div) h1 { color: #900; }
Run Code Online (Sandbox Code Playgroud)
这不是读",选择所有h1具有祖先不是div元素的元素......?" 因此,"你好世界!" 不应该是红色的,但它仍然是.
对于上面的标记,添加子组合器有效:
*:not(div) > h1 { color: #900; }
Run Code Online (Sandbox Code Playgroud)
但h1如果元素不是元素的子元素,则不会影响div元素.例如:
<div><article><h1>Hello World!</h1></article></div>
Run Code Online (Sandbox Code Playgroud)
这就是为什么我想将h1元素表示为元素的后代而不是子div元素.任何人?
以下是CSS3 :not()伪类的官方文档:
http://www.w3.org/TR/css3-selectors/#negation
和CSS Selectors Level 4增强功能:http:
//dev.w3.org/csswg/selectors4 /#否定
我一直在寻找实现和浏览器支持:not(),但我发现的唯一例子是单个元素或元素的直接子元素,例如:
div *:not(p) { color: red; }
Run Code Online (Sandbox Code Playgroud)
上面的例子适用于<p>直接的孩子<div>,但是当它<p>是一个更遥远的后代时它不起作用<div>.
div :not(p) {
color: red;
}Run Code Online (Sandbox Code Playgroud)
<div>
<ul>
<li>This is red</li>
</ul>
<p>This is NOT</p>
<blockquote><p>This is red but is not supposed to be!</p></blockquote>
</div>Run Code Online (Sandbox Code Playgroud)
如果答案在上面的官方文档中,那么我没有找到/理解它.正如我所说的,我搜索了这个网站和网站,但找不到任何关于支持或缺乏的讨论:not()作为另一个元素的孙子.
这应该像我认为的那样工作吗?
I have following scenario and I can't figure out how to select all p elements but not in div with content class:
<div class="content">
<p></p> <!-- not this one -->
<p></p> <!-- not this one -->
<div>
<p></p> <!-- not this one because this is also inside content -->
</div>
</div>
<div class="other">
<p></p> <!--this one -->
</div>
<p></p> <!--this one -->
Run Code Online (Sandbox Code Playgroud)
So I just want to select all p elements but not elements inside element with class="content".
More …