How to highlight active page in a masterpage menu?

后端 未结 6 1479
粉色の甜心
粉色の甜心 2020-12-10 17:24

I have a menu inside a masterpage (in a ASP.NET Web site), and i want to highlight active page in masterpage menu and submenus.

HTML:

&l         


        
6条回答
  •  無奈伤痛
    2020-12-10 17:36

    This works fine for me during development and in local, but when I host it on IIS the active highlighting on the menu does not work. What am I missing here? Masterpage code below

    $(document).ready(function () {
            //You can name this function anything you like
            function activePage() {
                //When user lands on your website location.pathname is equal to "/" and in 
                //that case it will add "active" class to all links
                //Therefore we are going to remove first character "/" from the pathname
                var currentPage = location.pathname;
                var slicedCurrentPage = currentPage.slice(1);
                //This will add active class to link for current page
                $('.nav-link').removeClass('active');
                $('a[href*="' + location.pathname + '"]').closest('li').addClass('active');
                //This will add active class to link for index page when user lands on your website
                if (location.pathname == "/") {
                    $('a[href*="index"]').closest('li').addClass('active');
                }
            }
            //Invoke function
            activePage();
        });
    

提交回复
热议问题