如何使用 jQuery 删除 <div> 的所有项目的所有 css 类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8932686/
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
How to remove all css classes of all items of a <div> with jQuery?
提问by Laguna
I have a div of nested ul/li items. each ul/li has a css class. Is there a clean wayto remove all css class for all items inside this <div>
?
我有一个嵌套的 ul/li 项目的 div。每个 ul/li 都有一个 css 类。有没有一种干净的方法来删除里面所有项目的所有 css 类<div>
?
edit: pls see here for code: http://jsfiddle.net/xjAkF/
编辑:请在此处查看代码:http: //jsfiddle.net/xjAkF/
回答by AlienWebguy
$('#myDiv ul, #myDiv li').removeClass();
回答by kwelch
Just use .removeClass()
if this is passed with no parameters it will remove all classes.
只需使用,.removeClass()
如果它不带参数传递,它将删除所有类。
You were missing the #
in the selector.
您缺少#
选择器中的 。
See revised code, here
在这里查看修改后的代码
@AlienWebguy, Has a better solution that way it only removes from the ul & li within the given id.
@AlienWebguy,有一个更好的解决方案,它只从给定 id 中的 ul 和 li 中删除。
回答by Dave
To strip out classes from ALL children of an element, use find("*")
要从元素的所有子元素中去除类,请使用 find("*")
$('#divHtml').find("*").removeClass();
回答by Anwar
$("#mydiv li, #mydiv ul").removeClass("Your class");
回答by jrummell
This should do it. Replace #myDiv with a selector for your div.
这应该这样做。将#myDiv 替换为您的 div 的选择器。
$("#myDiv ul, #myDiv li").each(function() { this.className = ""; });
回答by Mo.
$("#item").removeAttr('class');