使用javascript删除课程 [英] Remove class using javascript

查看:76
本文介绍了使用javascript删除课程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从页面中的所有元素中删除特定的class属性。那就是我们需要从当前html页面的所有元素中删除特定的类。我们只将类名作为参考(我们需要从该页面中删除)而不是其他任何内容。

I need to remove the specific class attribute from all the element in the page. That is we need to remove the particular class from all the elements of the current html page. We only have the class name as the reference (which we need to remove from that page) and nothing else.

请注意我不能使用任何内容作为参考喜欢id或其他类,而且我也不能使用任何JavaScript库...

Please note that i cannot use anything as reference like id or another class and also i cannot use any JavaScript libraries for this also...

对此的任何建议都将非常感谢。

Any suggestion on this would be greatly appreciated.

另请注意,我们需要移除的类不会超过两次。

Also note that the class we need to remove will not be there more than twice.

推荐答案

function removeClass(className) {
    // convert the result to an Array object
    var els = Array.prototype.slice.call(
        document.getElementsByClassName(className)
    );
    for (var i = 0, l = els.length; i < l; i++) {
        var el = els[i];
        el.className = el.className.replace(
            new RegExp('(^|\\s+)' + className + '(\\s+|$)', 'g'),
            '$1'
        );
    }
}

只需将其用作:

removeClass('desired-class-name');

UPDATE :查看现场演示: http://jsbin.com/eyuxur

这篇关于使用javascript删除课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆