简体   繁体   English

如何在 SB-Admin 引导模板中突出显示选定的导航菜单?

[英]How to highlight the selected navigation menu in SB-Admin bootstrap Template?

I am using SB-ADMIN bootstrap template in my project.我在我的项目中使用 SB-ADMIN 引导程序模板。 I want to high light the selected Menu.我想突出显示选定的菜单。 I tried this code, the navigation menu will be highlighted But the right side container it's not loaded.我试过这段代码,导航菜单将突出显示但右侧容器未加载。 Please find the attached file.请找到附件。

$("#exampleAccordion").on("click", function(event){
    $("#exampleAccordion").find("li").removeClass('active');
    $(event.target).closest("li").addClass('active');
    event.preventDefault();
});

在此处输入图片说明

Its better to handle it in the script side.最好在脚本端处理。 If you still want to use js for it, then use the following code.如果你还想用js来做,那就用下面的代码吧。 This should be attached to each page.这应该附在每一页上。

Assumes that you are using relative url for menu link.假设您使用相对 url 作为菜单链接。 like /abc/cdf/chart.html ./abc/cdf/chart.html If not change the logic in the $(this).attr('href') == window.location.pathname condition如果不改变$(this).attr('href') == window.location.pathname条件中的逻辑

$(document).ready(function(){
    $("#exampleAccordion a").each(function (index, element) {
        if ($(this).attr('href') == window.location.pathname) {
            $(this).parent().addClass("active");
        }
    });
});

The code will check the current url path with the menu urls, and if finds any match, then it will add class( active ) to the parent代码将使用菜单 url 检查当前 url 路径,如果找到任何匹配项,则将 class( active ) 添加到父级

Following with the suggestion from @Aneesh but for the SB Admin 2, considering the nested collapsed items, I came up with this:按照@Aneesh 的建议,但对于 SB Admin 2,考虑到嵌套的折叠项目,我想出了这个:

$(document).ready(function() {
    console.log(window.location.pathname);
    $("#accordionSidebar a").each(function(index, element) {
        if ($(element).attr('href') == window.location.pathname) {
            if ($(element).hasClass('collapse-item') == true) {
                $(element).parent().parent().parent().addClass("active");
                $(element).parent().parent().addClass("show");
                $(element).addClass("active");
            } else {
                $(element).parent().addClass("active");
            }
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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