2016-09-26 159 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