通过类名(不是 ID)添加/删除类
我想知道如何使用 jquery 按类名删除类。我知道如何通过 ID 进行操作。我正在尝试这样做:
var ButtonNumber = document.querySelectorAll(".ButtonStyleClicked");
for (var i = 0; i < ButtonNumber.length; i++) {
var ClassName = ButtonNumber[i];
$('.' + ClassName).removeClass('ButtonStyleClicked');
}
按 ID 删除类的工作原理如下:
var ID = document.getElementById(Whatever);
$('#' + ID).removeClass('ButtonStyleClicked');
解决了问题:
querySelectorAll 正在返回 DIV 元素。所以我将结果粘贴到删除类中并且有效:
var ButtonNumber = document.querySelectorAll(".ButtonStyleClicked");
for (var i = 0; i < ButtonNumber.length; i++) {
var ClassName = ButtonNumber[i];
$(ClassName).removeClass('ButtonStyleClicked');
}
I would like to know how to removeClass by Class Name using jquery. I know how to do it by ID. I'm trying this:
var ButtonNumber = document.querySelectorAll(".ButtonStyleClicked");
for (var i = 0; i < ButtonNumber.length; i++) {
var ClassName = ButtonNumber[i];
$('.' + ClassName).removeClass('ButtonStyleClicked');
}
Removing a class by ID works like this:
var ID = document.getElementById(Whatever);
$('#' + ID).removeClass('ButtonStyleClicked');
Solved the problem:
The querySelectorAll was shooting back an DIV element. So I stuck just the result into the remove class and that worked:
var ButtonNumber = document.querySelectorAll(".ButtonStyleClicked");
for (var i = 0; i < ButtonNumber.length; i++) {
var ClassName = ButtonNumber[i];
$(ClassName).removeClass('ButtonStyleClicked');
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否正在尝试查找具有
ButtonStyleClicked
类的元素以从中删除该类?(如果需要将类名存储在变量中:)
Are you trying to find elements with the
ButtonStyleClicked
class to remove the class from?(If you need to store the class name in a variable:)
我想说你可以使用任何选择器。假设
您可以根据需要使用任意数量的选择器项目和组合。例如;如果您只想从 id 为“myDiv”的
标记的子级
标记中删除某些类,那么它会是这样的:
I would say you can use whatever the selector is. Let's say
You can use as many selector items and combinations as needed. For example; in case you just want to remove certain class from the
<p>
tags that are children of a<div>
tag with an id of "myDiv", it would be something like: