简体   繁体   中英

How to add and remove active class?

How can i change class .active-link when i click on other anchors?

 <div id="settings" class="account-collapse collapse in">
     <div class="account-body">
    <a href="#/PersonalInfo" class="active-link">PERSONAL_INFORMATION</a>
    <a href="#/Notifications">NOTIFICATIONS_SETTINGS</a>
    <a href="#/PasswordChange">CHANGE_PASSWORD</a>
    <a href="#">GAME_SETTINGS</a>
     </div>
     </div>

I tried this but its not working:

    $('div').click(function () {
        var hash = window.location.hash;
        $("a.active-link").removeClass();
        $('a[href="' + hash + '"]').addClass('active-link');


});

Try

$('#settings a').click(function(event) {
   event.preventDefault(); // prevent default action
   $('#settings a').removeClass('active-link'); // remove all active class
   $(this).addClass('active-link'); // apply for current a tag
});

Fiddle

$('#settings a').not('.active-link').click(function(){//when other links are clicked
    $('#settings .active-link').removeClass('active-link').addClass('mynewclass');
})

DEMO: https://jsfiddle.net/fuk4cyk4/2/

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