admin管理员组文章数量:1303451
I am try to remove the same class on multiple ids
Here's the code:
var test = ['#step-1','#step-2','#step-3'];
$(test).removeClass('active-step');
This is not working, my question is..what's the problem or is there a better way of doing this?
I am try to remove the same class on multiple ids
Here's the code:
var test = ['#step-1','#step-2','#step-3'];
$(test).removeClass('active-step');
This is not working, my question is..what's the problem or is there a better way of doing this?
Share Improve this question asked Mar 24, 2016 at 9:50 Satch3000Satch3000 49.4k90 gold badges224 silver badges349 bronze badges 3- $('[id^=step]').removeClass("active-step"); – light_ray Commented Mar 24, 2016 at 9:51
- Shouldn't it just be one string? $('#step-1,#step-2,#step-3').... – paullb Commented Mar 24, 2016 at 9:54
- Jquery selector acts the same as CSS selector – T-moty Commented Mar 24, 2016 at 10:13
3 Answers
Reset to default 8Try to frame that array as a multiple selector
,
var test = ['#step-1','#step-2','#step-3'];
$(test.join()).removeClass('active-step');
As per the documentation, .join()
will merge the elements of an array by using ma(,
) if none supplied as its delimitter. So in our case, this signature would be useful in making a multiple selector.
separator : Optional. Specifies a string to separate each element of the array. The separator is converted to a string if necessary. If omitted, the array elements are separated with a ma. If separator is an empty string, all elements are joined without any characters in between them.
This is the correct jQuery syntax to select multiple ids:
$('#step-1, #step-2, #step-3').removeClass('active-step');
If you want to remove the classes of all elements whose id starts with step, then you can use starts ^
with attribute selector
$("[id^='step']").removeClass('active-step');
I thing your requirement is to remove the active calsses and add the active class to a particular element, then you can use like this too,
$(".active-step").removeClass('active-step');
本文标签: javascriptjQuery Remove same Class on Multiple Id39sStack Overflow
版权声明:本文标题:javascript - jQuery Remove same Class on Multiple Id's - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741759530a2396318.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论