2016-09-26 151 views
-1

为什么下面的代码无法正常工作?它应该隐藏所有不是p的元素,但display属性无法正常工作。:无(选择器)无法正常工作

p { 
 
    color: #000000; 
 
} 
 
:not(p) { 
 
    display: none; 
 
    color: #ff0000; 
 
}
<h1>This is a heading</h1> 
 

 
<p>This is a paragraph.</p> 
 
<p>This is another paragraph.</p> 
 

 
<div>This is some text in a div element.</div> 
 

 
<a href="http://www.w3schools.com" target="_blank">Link to W3Schools!</a>

+0

您必须指定:body:not(p) – Laurianti

+0

按预期工作。隐藏除“p”之外的所有元素,包括“p”的祖先。 – Oriol

+0

@Oriol代码具有正确的行为,但不是OP正在寻找的;) – dippas

回答

3

鉴于你的榜样,您的要求

应该隐藏所有不属于p

你必须使用body :not(p)元素 - 这意味着您正在使用*not()这样body *:not(p) - 这样宣称,它将应用样式到body的所有孩子除了p

body *:not(p) { 
 
    display: none; 
 
    color: #f00; 
 
} 
 
p { 
 
    color: #000; 
 
}
<h1>This is a heading</h1> 
 

 
<p>This is a paragraph.</p> 
 
<p>This is another paragraph.</p> 
 

 
<div>This is some text in a div element.</div> 
 

 
<a href="http://www.w3schools.com" target="_blank">Link to W3Schools!</a>

0

div *:not(p) em {…}

这将选择所有EM元素(不是ap元素)和元素中的元素。所以...是匹配,但是

...

不是。

可能你应该包括该部门。 My reference