简体   繁体   English

jQuery addClass仅添加第一个元素的类

[英]jQuery addClass only add the class for the first element

I want to add a class to all elements which have a defined class name. 我想向具有已定义类名的所有元素添加一个类。

There is my code: 有我的代码:

$(settings.slidesPlaceholder + ' ' + settings.slideIdentifier)
        .removeClass('active-slide')
        .addClass('inactive-slide');

But the issue is that the class is added only for the first element, but is removed from all elements. 但是问题在于,仅为第一个元素添加了该类,但从所有元素中删除了该类。

Please just make sure, you are not using id attribute for changing class . 请确保没有使用id属性更改class

For example: 例如:

$("#divid").removeClass('a').addClass('b');
$(body).removeClass('a').addClass('b');

The above will gonna add Class only to one element. 以上将仅将Class添加到一个元素。

$(".cldiv").removeClass('a').addClass('b');

The above will update all elements with class cldiv . 上面将用cldiv类更新所有元素。

So just console.log your settings.slidesPlaceholder+''+settings.slideIdentifier 所以只需console.log您的settings.slidesPlaceholder+''+settings.slideIdentifier

and see whether its a class , id or element . 看看它是一个classid还是element

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM