javascript 如何使用jquery更改所有孩子的班级?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9688491/
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 change class of all children with jquery?
提问by David
I have the following syntax in which I want to remove all css classes from the link tags. How can I do this using jquery?
我有以下语法,我想从链接标签中删除所有 css 类。我如何使用 jquery 做到这一点?
<div id="nav">
<ul>
<li><a href="" class="foo">aa</a></li>
<li><a href="" class="bar">aa</a></li>
<li><a href="" class="yay">aa</a></li>
</ul>
</div>
回答by ThiefMaster
You can use the removeClass()
method; if you call it without an argument it removes all classes:
您可以使用该removeClass()
方法;如果你不带参数调用它,它会删除所有类:
$('#nav ul a').removeClass();
Description: Remove a single class, multiple classes, or all classes from each element in the set of matched elements.
描述:从匹配元素集中的每个元素中删除单个类、多个类或所有类。
Obviously you can chain .addClass('someClass')
in the same call; you asked to changethe class in your question's subject after all. ;)
显然,您可以.addClass('someClass')
在同一个调用中进行链接;毕竟,您要求更改问题主题的课程。;)
回答by Brad Christie
$('#nav ul li a').each(function(){
this.className = '';
});
Finds all anchors, based on div#nav
selector, and removes all class names.
根据div#nav
选择器查找所有锚点,并删除所有类名。
回答by draconis
You can use $("a").removeClass();
您可以使用 $("a").removeClass();
here is example: http://jsfiddle.net/UPsEK/
这是示例:http: //jsfiddle.net/UPsEK/
回答by thelastshadow
If you want to remove every class then it's this:
如果你想删除每个类,那么它是这样的:
$('#nav a').each(function(){
$(this).removeAttr('class');
});