I have some divs:
我有一些div:
<div class="A">"Target"</div>
<div class="A B">"NotMyTarget"</div>
<div class="A C">"NotMyTarget"</div>
<div class="A D">"NotMyTarget"</div>
<div class="A E">"NotMyTarget"</div>
Is there a CSS selector that will get me the div containing Target
but not the divs containing NotMyTarget
?
是否有一个CSS选择器,它将获取包含Target但不包含NotMyTarget的div的div?
Solution must work on IE7, IE8, Safari, Chrome, and Firefox
解决方案必须适用于IE7,IE8,Safari,Chrome和Firefox
Edit: So far Nick is the closest. It's clumsy and I don't like the solution, but at least it works:
编辑:到目前为止尼克是最接近的。它很笨拙,我不喜欢这个解决方案,但至少它有效:
.A
{
/* style that all divs will take */
}
div.B
{
/* style that will override style .A */
}
3 个解决方案
#1
37
You can use the attribute selector to match the div
that has only one class:
您可以使用属性选择器来匹配只有一个类的div:
div[class=A] {
background: 1px solid #0f0;
}
If you want to select another div
that has multiple classes, use quotes:
如果要选择具有多个类的另一个div,请使用引号:
div[class="A C"] {
background: 1px solid #00f;
}
Some browsers do not support the attribute selector syntax. As usual, "some browsers" is a euphemism for IE 6 and older.
某些浏览器不支持属性选择器语法。像往常一样,“一些浏览器”是IE 6及更早版本的委婉说法。
See also: http://www.quirksmode.org/css/selector_attribute.html
另见:http://www.quirksmode.org/css/selector_attribute.html
Full example:
完整示例:
<!DOCTYPE html>
<html>
<head>
<style>
.A { font-size:22px; }
.B { font-weight: bold; border: 1px solid blue; }
.C { color: green; }
div[class="A"] {
border: 1px solid red;
}
div[class="A B"] {
border: 3px solid green;
}
</style>
</head>
<body>
<div class="A">"Target"</div>
<div class="A B">"NotMyTarget"</div>
<div class="A C">"NotMyTarget"</div>
<div class="A D">"NotMyTarget"</div>
<div class="A E">"NotMyTarget"</div>
</body>
</html>
EDIT 2014-02-21: Four years later, :not
is now widely available, though verbose in this specific case:
编辑2014-02-21:四年后,:现在没有广泛使用,虽然在这个特定情况下冗长:
.A:not(.B):not(.C):not(.D):not(.E) {
border: 1px solid red;
}
Unfortunately, this doesn't work in IE 7–8 as specified in the question: http://caniuse.com/#search=:not
不幸的是,这在问题中指定的IE 7-8中不起作用:http://caniuse.com/#search=:not
#2
27
.A:not(.B) {}
But guess who doesn't support that... Indeed, IE<=8.
但猜猜谁不支持......确实,IE <= 8。
#3
15
I think the best you can do (until CSS 3) is override the styles in this case with what you don't want from class A B
in A
, like this:
我认为你能做到的最好(直到CSS 3)在这种情况下用你不想要的A A中的A类来覆盖样式,如下所示:
.A.B { /* Styles */ }
.A { /* Reverse any styling you don't want */ }
#1
37
You can use the attribute selector to match the div
that has only one class:
您可以使用属性选择器来匹配只有一个类的div:
div[class=A] {
background: 1px solid #0f0;
}
If you want to select another div
that has multiple classes, use quotes:
如果要选择具有多个类的另一个div,请使用引号:
div[class="A C"] {
background: 1px solid #00f;
}
Some browsers do not support the attribute selector syntax. As usual, "some browsers" is a euphemism for IE 6 and older.
某些浏览器不支持属性选择器语法。像往常一样,“一些浏览器”是IE 6及更早版本的委婉说法。
See also: http://www.quirksmode.org/css/selector_attribute.html
另见:http://www.quirksmode.org/css/selector_attribute.html
Full example:
完整示例:
<!DOCTYPE html>
<html>
<head>
<style>
.A { font-size:22px; }
.B { font-weight: bold; border: 1px solid blue; }
.C { color: green; }
div[class="A"] {
border: 1px solid red;
}
div[class="A B"] {
border: 3px solid green;
}
</style>
</head>
<body>
<div class="A">"Target"</div>
<div class="A B">"NotMyTarget"</div>
<div class="A C">"NotMyTarget"</div>
<div class="A D">"NotMyTarget"</div>
<div class="A E">"NotMyTarget"</div>
</body>
</html>
EDIT 2014-02-21: Four years later, :not
is now widely available, though verbose in this specific case:
编辑2014-02-21:四年后,:现在没有广泛使用,虽然在这个特定情况下冗长:
.A:not(.B):not(.C):not(.D):not(.E) {
border: 1px solid red;
}
Unfortunately, this doesn't work in IE 7–8 as specified in the question: http://caniuse.com/#search=:not
不幸的是,这在问题中指定的IE 7-8中不起作用:http://caniuse.com/#search=:not
#2
27
.A:not(.B) {}
But guess who doesn't support that... Indeed, IE<=8.
但猜猜谁不支持......确实,IE <= 8。
#3
15
I think the best you can do (until CSS 3) is override the styles in this case with what you don't want from class A B
in A
, like this:
我认为你能做到的最好(直到CSS 3)在这种情况下用你不想要的A A中的A类来覆盖样式,如下所示:
.A.B { /* Styles */ }
.A { /* Reverse any styling you don't want */ }