如何在 javascript/jquery 中删除一个类的所有实例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17662550/
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 instances of a class in javascript/jquery?
提问by jdc987
I have this class called .m-active that is used multiple times throughout my HTML.
我有一个名为 .m-active 的类,它在我的 HTML 中多次使用。
Basically what I want to do is remove all instances of that class when a user clicks on an image (which does not have the m-active class) and add the m-active class to that image.
基本上我想要做的是当用户单击图像(没有 m-active 类)时删除该类的所有实例并将 m-active 类添加到该图像。
For instance in a Backgrid row you might have a click handler as follows:
例如,在 Backgrid 行中,您可能有一个单击处理程序,如下所示:
"click": function () {
this.$el.addClass('m-active');
}
But you also want to remove that class from any rows to which it was previously added, so that only one row at a time has the .m-active class
但是您还想从之前添加该类的任何行中删除该类,以便一次只有一行具有 .m-active 类
Does anyone know how this can be done in javascript/jquery?
有谁知道如何在 javascript/jquery 中做到这一点?
回答by jsalonen
With jQuery:
使用 jQuery:
$('.m-active').removeClass('m-active');
Explanation:
解释:
- Calling
$('.m-active')
selects all elements from the document that contain classm-active
- Whatever you chain after this selector gets applied to all selected elements
- Chaining the call with
removeClass('m-active')
removes classm-active
from all of the selected elements
- 调用
$('.m-active')
从包含类的文档中选择所有元素m-active
- 在此选择器应用于所有选定元素后链接的任何内容
- 将调用与从所有选定元素中
removeClass('m-active')
删除类m-active
For documentation on this specific method, see: http://api.jquery.com/removeClass/
有关此特定方法的文档,请参阅:http: //api.jquery.com/removeClass/
Getting grasp of the whole selector thing with jQuery is challenging at first, but once you get it, you see everything in very different light. I encourage you to take a look into some good jQuery tutorials. I personally recommend checking out Codeacademy's jQuery track: http://www.codecademy.com/tracks/jquery
一开始要掌握 jQuery 的整个选择器内容是具有挑战性的,但是一旦掌握了它,您就会以非常不同的方式看待一切。我鼓励您查看一些好的 jQuery 教程。我个人建议查看 Codeacademy 的 jQuery 轨道:http: //www.codecademy.com/tracks/jquery
回答by Alireza
all answers point to remove the class from the DOM element. But if you are asking to remove the element itself you can user .remove()
jquery method
所有答案都指向从 DOM 元素中删除类。但是如果您要求删除元素本身,您可以使用.remove()
jquery 方法
$('.m-active').remove();
回答by HIRA THAKUR
Jquery-:
jQuery-:
$("class").removeClass("your class");
javascript-:Set the class name to nothing when you want to remove class in javascript!!!
javascript-:当你想在javascript中删除类时,将类名设置为空!!!
document.getElementById("your id").className = "";
or
或者
element.classList.remove("class name");
回答by Dexygen
Specifically addressing the code block added to strengthen the quality of the question, and borrowing from jsalonen:
具体解决为加强问题质量而添加的代码块,并借用jsalonen:
"click": function () {
$('.m-active').removeClass('m-active');
this.$el.addClass('m-active');
}