开发者

addClass/removeClass by Class Name(not ID)

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 th开发者_如何学Gois:

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');

    } 


Are you trying to find elements with the ButtonStyleClicked class to remove the class from?

$('.ButtonStyleClicked').removeClass('ButtonStyleClicked');

(If you need to store the class name in a variable:)

var className = 'ButtonStyleClicked';
$('.' + className).removeClass(className);


I would say you can use whatever the selector is. Let's say

var className = "myClass";
$("."+className).removeClass(className);

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:

$("div#myDiv p."+className).removeClass(className);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜