我有这个布局:
<div id="sectors">
<h1>Sectors</h1>
<div id="s7-1103" class="alpha"></div>
<div id="s8-1104" class="alpha"></div>
<div id="s1-7605" class="beta"></div>
<div id="s0-7479"></div>
<div id="s2-6528" class="gamma"></div>
<div id="s0-4444"></div>
</div>
使用以下的CSS规则:
#sectors {
width: 584px;
background-color: #ffd;
margin: 1.5em;
border: 4px dashed #000;
padding: 16px;
overflow: auto;
}
#sectors > h1 {
font-size: 2em;
font-weight: bold;
text-align: center;
}
#sectors > div {
float: left;
position: relative;
width: 180px;
height: 240px;
margin: 16px 0 0 16px;
border-style: solid;
border-width: 2px;
}
#sectors > div::after {
display: block;
position: absolute;
width: 100%;
bottom: 0;
font-weight: bold;
text-align: center;
text-transform: capitalize;
background-color: rgba(255, 255, 255, 0.8);
border-top: 2px solid;
content: attr(id) ' - ' attr(class);
}
#sectors > div:nth-of-type(3n+1) {
margin-left: 0;
}
#sectors > div.alpha { color: #b00; background-color: #ffe0d9; }
#sectors > div.beta { color: #05b; background-color: #c0edff; }
#sectors > div.gamma { color: #362; background-color: #d4f6c3; }
我使用jQuery将未分配(unassigned)
类添加到那些没有alpha
、beta
或gamma
类中的扇区:
我使用 jQuery 在那些没有 alpha
、beta
或 gamma
类的扇区中添加了 unassigned
类。
$('#sectors > div:not(.alpha, .beta, .gamma)').addClass('unassigned');
然后我对那个类应用一些不同的规则:
#sectors > div.unassigned {
color: #808080;
background-color: #e9e9e9;
opacity: 0.5;
}
#sectors > div.unassigned::after {
content: attr(id) ' - Unassigned';
}
#sectors > div.unassigned:hover {
opacity: 1.0;
}
现代浏览器都可以完美运行。
考虑到 jQuery 中的 :not()
选择器 基于 CSS3 中的 :not()
,我想直接将其移入样式表中,这样就不必依赖于使用 jQuery 添加额外的类。此外,我也不是很关心支持旧版本的 IE,因为其他浏览器对 :not()
选择器的支持非常好。
因此,我尝试将上述部分的 .unassigned
更改为以下内容(知道我的布局中只有Α、Β和Γ三个部分):
#sectors > div:not(.alpha, .beta, .gamma) {
color: #808080;
background-color: #e9e9e9;
opacity: 0.5;
}
#sectors > div:not(.alpha, .beta, .gamma)::after {
content: attr(id) ' - Unassigned';
}
#sectors > div:not(.alpha, .beta, .gamma):hover {
opacity: 1.0;
}
但是,一旦我这样做,它就停止工作了——在所有浏览器中都是如此!我的未分配扇区不再是灰色的、被淡化的或标记为“未分配”。
为什么:not()
选择器在jQuery中可以正常工作,但在CSS中失败呢?既然jQuery声称是“符合CSS3标准”的,那么它在两个地方应该都能够完全一致地工作,难道我漏了什么吗?
这个问题有没有纯CSS的解决方法,还是我必须依靠脚本来实现?