简体   繁体   English

刷新页面后如何使我的导航栏保持活动状态?

[英]how to make my navbar stay active after refresh page?

I have navbar dropdown and I want to make this dropdown list will active when clicked, my JavaScript coding is running but it will disappear when page refresh, how to solve this problem???我有导航栏下拉菜单,我想让这个下拉列表在点击时激活,我的 JavaScript 编码正在运行,但在页面刷新时它会消失,如何解决这个问题???

My JavaScript:我的 JavaScript:

var header = document.getElementById("kt_header_menu");
var menu = header.getElementsByClassName("dropdown-item");
for (var i = 0; i < menu.length; i++) {
    menu[i].addEventListener("click", function() {
        var current = document.getElementsByClassName("active");
        if (current.length > 0) {
            current[0].className = current[0].className.replace(" active", "");
        }
        this.className += " active";
    });
}

localStorage.setItem('selectedTab', 'active');
let tabValue = localStorage.getItem('selectedTab');
if(tabValue !== '') {
  $('dropdown-item').addClass(tabValue);
} else {
  tabValue = '';
}

My html navbar:我的 html 导航栏:

<ul class="menu-nav ">
                    <li class="menu-item dropdown ">
                        <a class="nav-link dropdown-toggle text-dark" href="#" id="navbarDropdown" role="button"
                            data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            {{__('navbar.product')}}
                        </a>
                        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                            <a class="dropdown-item"
                                href="{{ route('product',['lang'=>App::currentLocale(), 'slug'=>'FMS']) }}">{{__('Fleet
                                Management System')}}</a>
                            <a class="dropdown-item "
                                href="{{ route('product',['lang'=>App::currentLocale(), 'slug'=>'TMS']) }}">{{__('Tranportation
                                Management System')}}</a>
                            <a class="dropdown-item"
                                href="{{ route('product',['lang'=>App::currentLocale(), 'slug'=>'TAS']) }}">{{__('Truck
                                Appointment System')}}</a>
                            <a class="dropdown-item"
                                href="{{ route('product',['lang'=>App::currentLocale(), 'slug'=>'IOS']) }}">{{__('Integration
                                to other system')}}</a>
                        </div>
                    </li>
                </ul>

now i know the answer just add active in a class and isset现在我知道答案只需在 class 和 isset 中添加 active

                            <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                            <a class="dropdown-item {{(isset($slug)?(($slug=='FMS')?'active':''):'')}}"
                                href="{{ route('product',['lang'=>App::currentLocale(), 'slug'=>'FMS']) }}">{{__('Fleet
                                Management System')}}</a>
                            <a class="dropdown-item {{(isset($slug)?(($slug=='TMS')?'active':''):'')}}"
                                href="{{ route('product',['lang'=>App::currentLocale(), 'slug'=>'TMS']) }}">{{__('Tranportation
                                Management System')}}</a>
                                href="{{ route('product',['lang'=>App::currentLocale(), 'slug'=>'TAS']) }}">{{__('Truck
                                Appointment System')}}</a>
                            <a class="dropdown-item {{(isset($slug)?(($slug=='IOS')?'active':''):'')}}"
                                href="{{ route('product',['lang'=>App::currentLocale(), 'slug'=>'IOS']) }}">{{__('Integration
                                to other system')}}</a>
                        </div>

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

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