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

后端 未结 9 1303
耶瑟儿~
耶瑟儿~ 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:56

    Actually Reynaldo, almost had it... At least for me, this works pretty good, on activing the current option and deactiving the previous one, based on his example.

    $(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).parent().addClass('active');
             }
        });
    });
    

提交回复
热议问题