【发布时间】:2021-03-19 20:11:40
【问题描述】:
我在使用 CSS 中的 ':not' 选择器时遇到问题,我想选择 head 下的所有内容,除了标题 (h1) 我尝试使用 '#head:not(h1)' 但它仍然选择了下面的所有内容#头。选择除 h1 之外的所有内容的正确方法是什么?谢谢。
<html>
<head>
<style>
/*I want to select all the elements under head id except for h1 title*/
#head:not(h1){
background: red;
}
</style>
</head>
<body>
<div id = "subpage">
<div id = "head">
<hgroup>
<h1>My Webpage</h1>
<h3>Subheading</h3>
</hgroup>
<div class = "posts">
<ul id = "post1">
<li>entered by: John</li>
<li>hearts: 2000</li>
</ul>
</div>
<div class = "posts">
<ul id = "post2">
<li>entered by: Chris</li>
<li>hearts: 100</li>
</ul>
</div>
</div>
<div id = "whats new">
<h1>Title</h1>
<p>a bunch of text</p>
</div>
</div>
</body>
</html>
【问题讨论】:
-
您当前的 CSS 选择器仅针对 ID 为
head的元素,而不是您想要的元素的子元素 - 考虑在#head和:not(h1)之间添加一个空格(即 @987654321 @) 来解决问题。 -
我很困惑“选择除一个之外的每个 div 元素”与选择除
h1标签之外的所有内容有何关联