Javascript Js从元素中删除所有类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/30299693/
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
Js remove all classes from element
提问by AmmarCSE
I have a button and when I click on it I want to remove all the classes. That's what I've tried so far:
我有一个按钮,当我点击它时,我想删除所有的类。这就是我迄今为止尝试过的:
button.style.className = ''
document.querySelector('button').onclick = function() {
  this.style.className = '';
}.myClass {
  color:red;
  
}
.second {
  color:green;
}<button class="myClass second">click me</button>Now I can't use classList.remove because I doen't know the class names, they are dynamic.
现在我不能使用 classList.remove 因为我不知道类名,它们是动态的。
How can I remove all the classes from an element?
如何从元素中删除所有类?
回答by AmmarCSE
Do not access classNamefrom the styleobject, access it directly like
不要className从style对象访问,直接访问它就像
this.className
document.querySelector('button').onclick = function() {
  this.className = '';
}.myClass {
  color:red;
  
}
.second {
  color:green;
}<button id="button" class="myClass second">click me</button>回答by chris97ong
回答by BJovke
HTML DOM removeAttribute()Method:
HTML DOMremoveAttribute()方法:
document.querySelector('button').onclick = function() {
  this.removeAttribute("class");
}.myClass {
  background-color:red;
}
.second {
  box-shadow: 0px 0px 10px 10px;
}<button id="button" class="myClass second">click me</button>
