Javascript 如何从div中删除类属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5169017/
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-23 15:55:04  来源:igfitidea点击:

How to remove class attribute from div?

javascript

提问by 3gwebtrain

I am using JavaScript and I want to add/remove a Class attribute if a button is clicked. I am able to add the class, but I don't know how to remove it. how can I do that?

我正在使用 JavaScript,如果单击按钮,我想添加/删除 Class 属性。我可以添加课程,但我不知道如何删除它。我怎样才能做到这一点?

window.onload = function(){
    var buttonGo = document.getElementsByTagName('button')[0];
    var buttonCom = document.getElementsByTagName('button')[1];
    var box = document.getElementById('box');
    buttonGo.onclick = function(){
        box.setAttribute('class','move');
    }
    buttonCom.onclick = function(){
        // how to remove class name?
    }
}

回答by BJ Safdie

box.removeAttribute("class")should work.

box.removeAttribute("class")应该管用。

回答by lonesomeday

The nicest way to set classes with Javascript is to use the classNameproperty:

使用 Javascript 设置类的最好方法是使用className属性:

// to add
box.className = 'move';
// to remove
box.className = '';

回答by u476945

function hasClass(ele,cls) {
    return ele.className.match(new RegExp('(\s|^)'+cls+'(\s|$)'));
}
function addClass(ele,cls) {
    if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}
function removeClass(ele,cls) {
    if (hasClass(ele,cls)) {
        var reg = new RegExp('(\s|^)'+cls+'(\s|$)');
        ele.className=ele.className.replace(reg,' ');
    }
}

You can use RegExp in theses three functions to check class existance, add class, and remove class. Here is the source openjs

您可以在这三个函数中使用 RegExp 来检查类是否存在、添加类和删除类。这是源代码openjs

回答by ynkr

In the future-ish, you can also consider Element.classList.

在未来,您还可以考虑Element.classList

   var d = document.getElementsByTagName('div')[0];
   d.classList; // []
   d.classList.add('foo'); // undefined
   d.classList; // ["foo"]
   d.classList.remove('foo'); // undefined
   d.classList; // []

I say future-ish b/c you have to consider this IE < 10.

我说未来的 b/c 你必须考虑这个 IE < 10。

回答by ant

Try this :

尝试这个 :

window.onload = function(){
            var buttonGo = document.getElementsByTagName('button')[0];
            var buttonCom = document.getElementsByTagName('button')[1];
            var box = document.getElementById('box');
            buttonGo.onclick = function(){
                box.setAttribute('class','move');
            }
            buttonCom.onclick = function(){
               box.className=''
            }
       }

or double quotes box.className=""

或双引号 box.className=""

回答by Brett Zamir

For cross-browser support:

对于跨浏览器支持:

buttonCom.onclick = function() {
    box.className = ''; // IE
    box.removeAttribute('class'); // Others
};

回答by user1146912

You should consider jQuery, than you can just use this: $('.itemclass').removeClass('classname');

你应该考虑 jQuery,而不是你可以使用这个: $('.itemclass').removeClass('classname');