How to set navbar item as active when user selects it?

后端 未结 9 1283
耶瑟儿~
耶瑟儿~ 2021-02-05 18:45

I am a new ASP.NET Web Forms developer and trying to use Twitter Bootstrap with the Master Page. I am struggling with setting navbar item as active when user selects it. I creat

9条回答
  •  礼貌的吻别
    2021-02-05 18:52

    I know this post is old but use

        $(document).ready(function () {
    
        var url = window.location;
        $('ul.nav li a').each(function () {
            if (this.href == url) {
                $("ul.nav li").each(function () {
                    if ($(this).hasClass("active")) {
                        $(this).removeClass("active");
                    }
                });
                $(this).parents().addClass('active');
            }
    
        });
    
    });
    

    Since you also wants to active the all the parents.

提交回复
热议问题