CSS:我可以选择不在特定类中的每种类型的元素吗?

CSS:我可以选择不在特定类中的每种类型的元素吗?,css,css-selectors,Css,Css Selectors,假设我得到了这一页: <body> <h1>Hello!</h1> <div class="wrapper"> <div class="anotherclass"> <h1>Another heading 1</h1> </div> <div class="yetanotherclass">

假设我得到了这一页:

<body>
    <h1>Hello!</h1>
    <div class="wrapper">
        <div class="anotherclass">
            <h1>Another heading 1</h1>
        </div>
        <div class="yetanotherclass">
            <h1>Yet another heading 1</h1>
        </div>
    </div>
    <h1>Good bye!</h1>
    <div class="class">
        <h1>Good bye. And this time I mean it.</h1>
    </div>
</body>
我更喜欢这样:

h1:not(.wrapper > h1) {style definitions}

有什么方法可以做到这一点吗?

如果你做了这样的事情会怎么样:

h1 { /* rules for everything without the class */ }
h1.class { /* rules for everything with the class */ }
在h1.class中,您将覆盖在h1规则中定义的所有内容

以下是一个例子:

<html>
    <head>
        <style type="text/css">
                div { color:#00f; }
                div.foo { color:#f00; }
        </style>
    </head>
    <body>
        <div class="foo">foo</div>
        <div>bar</div>
        <div>baz</div>
    </body>
</html>

在本例中,我有效地针对了所有没有foo类的div。

您可以使用通用选择器*应用全局样式,然后应用嵌套的通用选择器:.wrapper*撤消最初应用的样式

* {font-weight:bold; border:thin solid red;}

.wrapper * {font-weight:normal; border: 0px solid white;}

css不能满足你的要求。围绕css的整个想法是级联,而您想要做的是与级联流相反的工作

与潮汐一起工作,做常规css:

h1 {style definitions}
.wrapper h1 {style definitions}

这在我目前的解决方案中是不可能的。我无法更改HTML代码,因为它加载了来自外部服务器的Javascript,所以我无法在标题上设置类。我编辑了答案以删除示例中的类。您根本不需要更改HTML,因为第一条规则将为所有元素设置样式,第二条规则将仅适用于具有要排除的类的元素。问题是,我将使用此选择器作为javascript的输入,javascript可以理解css选择器,但不理解那些嵌套的变体。它必须是一行内容,类似于jQuery中的css选择器。您当然可以在jQuery中添加目标父类等。这实际上是一个sIFR实现,我正在与富文本编辑器结合使用。富格文本编辑器位于包含h2和h3类型元素的特定div中,对于那些我不希望sIFR替换文本的元素,但是在该类型的所有其他出现的元素上。使用通用选择器作为键选择器是不好的做法;它正在降低性能。
h1 {style definitions}
.wrapper h1 {style definitions}