简体   繁体   中英

MVC Bootstrap Navbar - active class stays to just one <li> element

I have a bootstrap navbar in my asp.net mvc app, and all I am trying to do is to change the active class to the currently selected element from the navbar menu.

I managed to partly achieve this by using the following jquery :

$(document).ready(function () {
            $('ul.nav.navbar-nav').find('a[href="' + location.pathname + '"]')
                .closest('li').addClass('active');
            $(this).siblings().removeClass('active');
        });

After implementing this both of my navbar links are highlighter. At first, only the one with the default active class, then the one I click to some other link It also becomes highlighter, but the default active still stays active, so I end up with two active links.

Can someone help me make this work properly

Bootstrap:

<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>
                    @Html.ActionLink("blah blah", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
                </div>
                <div class="navbar-collapse collapse">
                    <ul class="nav navbar-nav">
                        <li>@Html.ActionLink("blah", "Create", "Todoes")</li>
                        <li class="active">@Html.ActionLink("ALL", "Index", "Todoes")</li>
                        <li>@Html.ActionLink("blah2", "", "Todoes")</li>
                        <li>@Html.ActionLink("blah3", "", "Todoes")</li>




                    </ul>
                </div>
            </div>

Why not just control this from razor in the .cshtml file?

I do it this way:

public string CheckActive(string actionName, string controllerName)
{
   return ((ViewContext.RouteData.Values["action"].ToString().ToLower() == actionName.ToLower() && ViewContext.RouteData.Values["controller"].ToString().ToLower() == controllerName.ToLower()) ? "active" : string.empty);
}

This method just returns active if the given actionName and controllerName matches the current Action and Controller.

this way you would use it like this:

<li class="@CheckActive("actionInActionLink", "controllerInActionLink")">@Html.ActionLink("Link text", "actionInActionLink", "controllerInActionLink")</li>

Please try this:

$(document).ready(function () {
        $('li').each(function(){
               $(this).removeClass('active');
        });
        $('ul.nav.navbar-nav').find('a[href="' + location.pathname + '"]').closest('li').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