Html 如何在特定 DIV 上禁用 CSS 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25863836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:42:40  来源:igfitidea点击:

How to disable CSS Class on particular DIV

htmlcssclassstyles

提问by Enigma

I want to disable class automatically applied on div.

我想禁用自动应用于 div 的类。

<div class="A B">

I want to disable Class 'A' but not 'B'. How could I do that ?

我想禁用“A”类而不是“B”类。我怎么能那样做?

回答by yunzen

I think you are looking for CSS that says. Don't listen to class A.

我认为您正在寻找说的 CSS。不要听A班。

Alas, there is no such CSS.

唉,没有这样的 CSS。

  • Either you have to remove the class A from your source code.
  • Or you remove the class from the DOM by means of JavaScript.
  • Or you must overwrite your CSS in class B that any value that was set in class A gets initial/neutral values (sometimes it's the value 'initial', sometimes its the value 'auto' or 'none')
  • 您必须从源代码中删除 A 类。
  • 或者您通过 JavaScript 从 DOM 中删除该类。
  • 或者,您必须覆盖 B 类中的 CSS,使 A 类中设置的任何值都获得初始/中性值(有时是“初始”值,有时是“自动”或“无”值)

回答by daker

You are probably looking for a javascript...

你可能正在寻找一个 javascript ......

Add a id to your div and use

将 id 添加到您的 div 并使用

<div id="whatever" class="A B"></div>

<script type="text/javascript">
    window.document.onload = function(){ 
        document.getElementById("whatever").className = "A";
    }     
</script>

回答by Enigma

I think that I accomplished succesefully. :)

我认为我成功地完成了。:)

For example I have my html code :

例如我有我的 html 代码:

<a class="m2" href="#">Some text here, with bold</a>
<a class="m2 nobold" href="#">Some text here, without bold</a>

I use CSS for the first text

我对第一个文本使用 CSS

.m2 {
    font-weight: bold;
}

And for disable it, I use

为了禁用它,我使用

.nobold {
font-weight: normal;
}

And it apply both, but you can change settings from the other CSS class ...

它同时适用,但您可以更改其他 CSS 类的设置......

回答by Matthew Shaile

If you want to do this in straight js, give the div an ID:

如果你想在直接的 js 中做到这一点,给 div 一个 ID:

<div id="myDiv" class="A B"></div>

And then in js:

然后在js中:

document.getElementById("myDiv").classList.remove("B");