How to change css for active link based on the hashtag

前端 未结 2 665
轻奢々
轻奢々 2021-01-22 02:37

I have a situation here on how to change my active link css based on the hashtag. All the content was in the same page and I use the #url section to call the content.

i

相关标签:
2条回答
  • 2021-01-22 03:33
        jQuery(function() {
          jQuery('a').each(function() {
            if (jQuery(this).attr('href')  ===  window.location.href) {
              jQuery(this).addClass('active');
            }
          });
        });  
    
    .active {
        color: #BBA842!important;
    }
    
    0 讨论(0)
  • 2021-01-22 03:38

    What I can suggest is using jQuery. Somthing like this.

    $(document).ready(function(){
    $('div#services_menu li').click(
        function(e)
        {
            $('div#services_menu li').removeClass('active');
            $(e.currentTarget).addClass('active');
        }
    );
    });
    
    
    li.active a
    {
        color: #BBA842!important;
    }
    

    Because, first of all you are using anchor links, and you want to make it dynamic. If any other person can suggest solution using CSS I will appreciate that answer.

    0 讨论(0)
提交回复
热议问题