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

后端 未结 9 1310
耶瑟儿~
耶瑟儿~ 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 19:05

    following code works if I have subpages:

     $(document).ready(function () {
    
            $('.navbar .nav').find('.active').removeClass('active');
    
            var url = window.location.toString();
            var u = url.substring(0, url.lastIndexOf("/")).toString();
    
            $('.navbar .nav li a').each(function () {
    
                var hr = this.href.substring(0, this.href.lastIndexOf('/')).toString();
    
                if ((u == hr) || (u.indexOf(hr) > -1))
                {
                    $(this).parent().addClass('active');
                    return false;
                }
            });
            // Deactivation to manually add items you have with href = "#" example:
            $('#liSalir').removeClass('active');
    });
    

提交回复
热议问题