Html 从特定DIV中删除所有CSS

Html 从特定DIV中删除所有CSS,html,css,Html,Css,可能重复: 我有一个页面,加载一个具有不同CSS属性的外部CSS文件 是否可以在同一个页面中创建一个元素,并且不加载任何css 例如: <style type="text/css"> p { background-color:#000000; width:550px; } </style> <p>This should get the P styling from the style tag</p> <p>This s

可能重复:

我有一个页面,加载一个具有不同CSS属性的外部CSS文件

是否可以在同一个页面中创建一个元素,并且不加载任何css

例如:

<style type="text/css">
p {
    background-color:#000000;
    width:550px;
}
</style>
<p>This should get the P styling from the style tag</p>
<p>This should NOT get the P styling</p>

p{
背景色:#000000;
宽度:550px;
}
这应该从样式标记中获取p样式

这不应该得到p样式


这正是类的设计目的

<style type="text/css">
.class1{
background-color:#000000;
width:550px;
}
</style>
<p class="class1">This should get the P styling from the style tag</p>
<p>This should NOT get the P styling</p>

.1级{
背景色:#000000;
宽度:550px;
}

这应该从样式标签中获得p样式

这不应该得到p样式


作为记录,不要使用class1这样的名称,因为它只是用于演示。对有意义的类使用描述性名称。

正如我所评论的,使用类、ID和伪选择器有什么错

例如,这很好:

p:first-child {
    background-color:#000000;
    width:550px;
}
同样

.first {background-color: #000; width: 550px;}

<p class="first">Some styled text</p>
<p>Some default text</p>
.first{背景色:#000;宽度:550px;}

一些样式化的文本

一些默认文本


您可以积极地隔离您想要设置样式的p:

<p class="hasStyle"></p
<p></p>

正如其他人所说,通常有更好的方法来隔离元素。然而,也有一个CSS选择器用于此目的

HTML 例如:


这很少是正确的解决方案,但它对于处理难以与其他选择器匹配的边缘情况非常有用。

使用类、ID和伪选择器有什么错?您可以使用JavaScript将类自动添加到现有元素中。如果有兴趣让我知道,我会给一个粗略的例子。这是如此基本。不过,不要使用JS,因为它是特定于大小写的,只适用于只需要第一个p样式的情况。您的前两个注释显然是更好的解决方案,类或ID,甚至ID,只有在正确使用它们的情况下才适用于单个实例。
<style>
p {
    background-color:#000000;
    width:550px;
}

.noStyle {
 background-color: none;
 width: none /* or whatever you want here */;
}
</style>

<p>has a style</p>
<p class="noStyle"></p>
<p>A paragraph</p>
<p class="nostyle">Don't style me</p>
<p>A paragraph</p>
<p>A paragraph</p>
P:not(.nostyle) { color: red; }