Add & remove active class from a navigation link

前端 未结 4 493
有刺的猬
有刺的猬 2020-12-18 03:50

I have been looking for tutorials on how to add and remove a class from a link unfortunately without any success. All the earlier questions on this have give me some underst

4条回答
  •  心在旅途
    2020-12-18 04:54

    Try this:

    $(function() {
        $('.start').addClass('active');
            $('#main-nav a').click(function() {
            $('#main-nav a').removeClass('active');
            $(this).addClass('active');             
       });
    });
    

    Just add the class .start to the nav element you want to have the class .active first.

    Then declare the class .active in your css like:

    #main-nav a.active {
    
    /* YOUR STYLING */
    
    }
    

提交回复
热议问题