![](/img/trans.png)
[英]How do I toggle a class on click of an element but remove the same class from all other elements using JQuery?
[英]On click add class to element with certain class and remove it from all other elements with same class
基本上我想弄清楚以下我有几个元素
<a href="#" clas="v-bar"></a>
<a href="#" clas="v-bar"></a>
<a href="#" clas="v-bar"></a>
<a href="#" clas="v-bar"></a>
<a href="#" clas="v-bar"></a>
我需要弄清楚如何在单击元素时为其添加类,例如添加.v-current
,但同时将其从具有.v-bar
类的所有其他元素中删除? 全部使用jQuery。
$('.v-bar').click(function(e){
$('.v-bar').removeClass('v-current');
$(this).addClass('v-current');
e.preventDefault();
});
$('.v-bar').click(function(){
$('.v-current').removeClass('v-current');
$(this).addClass('v-current');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.