Active navigation with jQuery - can't apply a default class to anchor

前端 未结 4 1371
天命终不由人
天命终不由人 2021-01-06 14:23

I am currently trying to make a navigation-menu where an active-class is applied to the anchors whose href attributes that match the c

4条回答
  •  北海茫月
    2021-01-06 14:50

    I think you can simplify this a bit:

    function highlightSelected()
    {
      $("h2.subnav a").each(
        function()
        {
          if (location.pathname.indexOf(this.href) > -1)
          {
            $(this).addClass("selected");
          }
        }
      );
    }
    

提交回复
热议问题