简体   繁体   中英

How To Highlight Show Active Tab In A Navbar-Fixed-Top

I am current changing a website to use Bootstrap and I have been advised that they want to use the navbar-inverse navbar-fixed-top main menu.

The thing is I can't figure out how to highlight the selected page and keep it highlighted.

I have tried the follow Jquery but I cant get none of them working

 $(function() {
    $('#nav li a').click(function() {
       $('#nav li').removeClass();
       $($(this).attr('href')).addClass('active');
    });
 });

My HTML for my menu is:

    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" runat="server" href="~/">Logo Here</a>
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li><a runat="server" href="~/"><span class="glyphicon glyphicon-home"></span></a></li>
                    <li style="border-left: 1px solid lightgray"><a runat="server" href="~/About">About</a></li>
                    <li style="border-left: 1px solid lightgray"><a runat="server" href="~/Session/pg1">Session</a></li>
                    <li style="border-left: 1px solid lightgray"><a runat="server" href="~/EmailPg">Email</a></li>
                </ul>
                <asp:LoginView runat="server" ViewStateMode="Disabled">
                    <AnonymousTemplate>
                        <ul class="nav navbar-nav navbar-right">
                            <li><a runat="server" href="~/Contact">Contact us</a></li>
                        </ul>
                    </AnonymousTemplate>
                </asp:LoginView>
            </div>
        </div>
    </div>

I would prefer if its possible to do it with CSS or Jquery but I was also a little confused as, do I add the Jquery to every page OR can I add one lot to my Site.Master file

You were not selecting the active li tag correctly.

Instead I suggest you use something like this:

$(function() {
    $('#nav li a').click(function() {
        $(this).closest('li') // select the parent <li> tag
        .addClass('active')// add the active class
        .siblings() // select the siblings of the active tag
        .removeClass('active'); // remove the active class from the other <li>
    });
});

Fixed by adding the below JQuery to my Site.Master file

var url = window.location;
// Will only work if string in href matches with location
$('ul.nav a[href="' + url + '"]').parent().addClass('active');

// Will also work for relative and absolute hrefs
$('ul.navbar-nav a').filter(function () {
    return this.href == url;
}).parent().addClass('active');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM