“ INSPINIA”管理主题上的活动侧面菜单

时间:2018-10-28 08:12:21

标签: javascript jquery inspinia

我想使用JavaScript在侧边栏菜单上突出显示活动页面。我正在使用INSPINIA主题。

我尝试使用JavaScript,对于普通菜单来说,它工作正常。但这不适用于子菜单。我想为子菜单显示in类而不是active类。请检查我的代码并为我提供帮助。

HTML

<nav class="navbar-default navbar-static-side" role="navigation">
   <div class="sidebar-collapse">
      <ul class="nav metismenu" id="side-menu">
         <li>
            <a href="dashboard.html"><i class="fa fa-th-large"></i><span class="nav-label">Dashboards</span></a>
         </li>
         <li>
            <a href="#"><i class="fa fa-building"></i><span class="nav-label">Company</span><span class="fa arrow"></span></a>
            <ul class="nav nav-second-level collapse">
               <li><a href="addcompany.html">Add Company</a></li>
               <li><a href="#">View Company</a></li>
            </ul>
         </li>
         <li>
            <a href="mailbox.html"><i class="fa fa-envelope"></i><span class="nav-label">Person</span><span class="fa arrow"></span></a>
            <ul class="nav nav-second-level collapse">
               <li><a href="addperson.html">Add Person</a></li>
               <li><a href="#">View Person</a></li>
            </ul>
         </li>
      </ul>
   </div>
</nav>

JavaScript

$(document).ready(function () {
    var url = window.location;
    // for sidebar menu but not for metismenu submenu
    $('ul.metismenu a').filter(function () {
        return this.href == url;
    }).parent().siblings().removeClass('active').end().addClass('active');
    // for metismenu which is like a submenu
    $('ul.nav-second-level a').filter(function () {
        return this.href == url;
    }).parentsUntil(".metismenu > .nav-second-level").siblings().removeClass('active').end().addClass('active');
});

预期产量

<li class="active">
   <a href="#"><i class="fa fa-building"></i><span class="nav-label">Company</span><span class="fa arrow"></span></a>
   <ul class="nav nav-second-level collapse in">
      <li><a href="addcompany.html">Add Company</a></li>
      <li><a href="#">View Company</a></li>
   </ul>
</li>

实际输出

<li class="active">
   <a href="#"><i class="fa fa-building"></i><span class="nav-label">Company</span><span class="fa arrow"></span></a>
   <ul class="nav nav-second-level collapse active">
      <li><a href="addcompany.html">Add Company</a></li>
      <li><a href="#">View Company</a></li>
   </ul>
</li>

3 个答案:

答案 0 :(得分:1)

您的功能不起作用。取而代之的是:

Code 1 // for customer A

Code 2  // for Customer B

code 3  // Shared code

答案 1 :(得分:0)

我找到了答案。

$(function() {
    setNavigation();
});

function setNavigation() {
    var path = window.location.pathname;
    path = path[0] == '/' ? path.substr(1) : path; //it will remove the dash in the URL

    $("ul.metismenu a").each(function() {
        var href = $(this).attr('href');
        if (path.substring(0, href.length) === href) {
            $(this).closest('li').addClass('active');
        }
    });

    $("ul.metismenu .nav-second-level a").each(function() {
        var href = $(this).attr('href');
        if (path.substring(0, href.length) === href) {
            $(this).parent().parent().closest('li').addClass('active');
            $(this).closest('ul').addClass('in');
        }
    });
}

答案 2 :(得分:0)

acmsohail的回答对我有所帮助,但是我发现一个问题,如果有两个以相同方式开头的URL,则扩展名最大的URL将显示两个选定的URL。 然后我留下带有正确元素的代码。

 function setNavigation() {
    var path = location.href;
    var lstElem = [];
    var classIndex;
    path = path[0] == '/' ? path.substr(1) : path;

    $("ul.metismenu a").each(function (i,e) {
        var href = $(this).attr('href');
        if (path.substring(0, href.length)  === href) {

            lstElem.push({
                element : e,
                count : href.length
            })

        }
    });

    if(lstElem.length > 0)
    {
        //i get the element with max length
        classIndex = $(lstElem.reduce(function(prev, current) {
            return (prev.count > current.count) ? prev : current
        })["element"]);

        classIndex.closest('li').addClass('active');

        lstElem = [];            
    }

    $("ul.metismenu .nav-second-level a").each(function () {
        var href = $(this).attr('href');
        if (path.substring(0, href.length) === href) {
            lstElem.push({
                element : e,
                count : href.length
            })
        }
    });

    if(lstElem.length > 0)
    {
        classIndex = $(lstElem.reduce(function(prev, current) {
            return (prev.count > current.count) ? prev : current
        })["element"]);

        classIndex.parent().parent().closest('li').addClass('active');
        classIndex.closest('ul').addClass('in');
    }
}