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

前端 未结 4 1374
天命终不由人
天命终不由人 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 15:00

    Give this code a shot, its something I put together for the company that I work for.

    // highlight tab function
    var path = location.pathname;
    var home = "/";
    $("a[href='" + [path || home] + "']").parents("li").each(function() {   
        $(this).addClass("selected");
    });
    

提交回复
热议问题