简体   繁体   中英

On click add class to element with certain class and remove it from all other elements with same class

Basically I am trying to figure out the following I have several elements like

<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>

I need to figure out how to add class to an element when it is clicked, so for example add .v-current , but at the same time remove it from all other elements with a class of .v-bar ? All with 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');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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