ASP.NET MVC: Masterpage: How to set css class on active menu item

前端 未结 5 1407
情书的邮戳
情书的邮戳 2020-12-23 23:25

I have the following menu in my masterpage:

  • <%= Html.ActionLink(\"Employees\",
5条回答
  •  萌比男神i
    2020-12-23 23:56

    Extract the current location from window.location. Then use a selector that specifies the value of the href attribute to choose just those elements that match (presumably only one).

     var currentLocation = window.location.href;
     // probably needs to be massaged to extract just the path so that it works in dev/prod
    
    $("#menu li a[href$="+currentLocation+"]").addClass("current");
    

提交回复
热议问题