jQuery 如何从所有子元素中删除类

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

How to remove class from all child elements

jqueryhtmlattr

提问by Shijin TR

I have a html like:

我有一个像:

<table id="message">
    <tr class="clsaa1">
        <td>
            <div class="class3">
                <table class="sub_1">
                ----------------------
                ----------------------
                </table>
            <div>
        </td>
    </tr>
    <tr class="clsaa2">
        <td>
            <div class="class3">
                ----------------------
                ----------------------
            <div>
        </td>
    </tr>
</table>

I need to remove allclassattributes inside #message.

我需要删除class里面的所有属性#message

I have tried

我试过了

$('#message').siblings().removeAttr('class');

and

$('#message').children().removeAttr('class');

But this is not working.

但这是行不通的。

回答by Adi

the simple and elegant solution would be just:

简单而优雅的解决方案就是:

$(".class").removeClass("class");

$(".class").removeClass("class");

回答by mithunsatheesh

Instead of removeAttr('class')you can use removeClass("classname")

而不是removeAttr('class')你可以使用removeClass("classname")

If you want to remove all the class values from its children, then do like

如果要从其子项中删除所有类值,请执行以下操作

$('#message').find("*").prop("class","");

or even

甚至

$('#message').find('*') removeAttr('class');

Here is a fiddle.

这是一个小提琴

回答by Patrick

Older thread, but in my case I wanted to remove a specific class from all child elements

较旧的线程,但在我的情况下,我想从所有子元素中删除特定的类

 $('#parentDiv').find("*").removeClass("row-selected");

回答by shenku

You need to use find to grab all the nested elements, then iterrate over them.

您需要使用 find 来获取所有嵌套元素,然后遍历它们。

$('#message').find('*').each(function(){
     $(this).removeAttr('class');
})

回答by Shijin TR

It will work fine

它会正常工作

    $('#message').find('*').removeAttr('class');