Dynamically change CSS of link based on current page

前端 未结 4 835
长发绾君心
长发绾君心 2020-11-29 12:56

I have the following links at the top of my web page:

 
4条回答
  •  一整个雨季
    2020-11-29 13:23

    You can check each link and see if it matches the current location. This can be more or less advanced depending on your needs, but something like:

    var loc = window.location.pathname;
    
    $('.icyLink').find('a').each(function() {
      $(this).toggleClass('active', $(this).attr('href') == loc);
    });
    

    Then style the active class in your CSS:

    .icyLink a.active{color:#fff}
    

提交回复
热议问题