页面加载菜单栏上的javascript活动类

时间:2017-01-30 11:26:35

标签: javascript jquery laravel

我使用Laravel建立了一个管理面板..我有侧栏有多个菜单.. 我面临的问题是,每当我点击其中一个菜单时,我想要激活该类,但它无法保持活跃状态​​。

这是我的侧边栏的代码

<div id="sidebar">   
    <ul>
        <li class="submenu"> <a href="#"><i class="icon icon-list"></i> <span>Members</span></a>
            <ul>
              <li class="submenu-2"><a href="{{asset('member/create')}}">Create Member</a></li>          
              <li class="submenu-2"><a href="{{asset('member')}}">Member List</a></li>               
            </ul>
        </li>
        <li class="submenu"> <a href="#"><i class="icon icon-list"></i> <span>Payroll</span></a>
            <ul>
                    <li class="submenu-2"><a href="{{asset('payroll/create')}}">Create Monthly Payroll</a></li>               
                    <li class="submenu-2"><a href="{{asset('managerPayroll')}}">Unapproved Monthly Payroll (Management)</a></li>                           
                    <li class="submenu-2"><a href="{{asset('nonManagerPayroll')}}">Unapproved Monthly Payroll (Employee)</a></li>
                    <li class="submenu-2"><a href="{{asset('printEmployeeAccount')}}">Print Employee Account</a></li>                     
            </ul>
        </li>
        <li class="submenu"> <a href="#"><i class="icon icon-list"></i> <span>Report</span></a>
            <ul>
                    <li class="submenu-2"><a href="{{asset('serachManagerReport')}}">Search Manager Report</a></li> 
                    <li class="submenu-2"><a href="{{asset('serachNonManagerReport')}}">Search Employee Report</a></li>
                    <li class="submenu-2"><a href="{{asset('salaryReport')}}">Salary Sheet </a></li> 
            </ul>
        </li>
        <li class="submenu-2"> <a href="{{asset('changePassword')}}"><i class="icon icon-list"></i> <span>Password Update</span></a>
        </li>
    </ul>

</div> 

我用过的javascript使网址变为活动状态。

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>         
<script>
    (function() {
    var nav = document.getElementById('sidebar'),
        anchor = nav.getElementsByTagName('a'),
        current = window.location.pathname.split('/')[1];
        for (var i = 0; i < anchor.length; i++) {
        if(anchor[i].href == current) {
            anchor[i].className = "active";
        }
    }
})();
</script>

有人可以在这里建议什么是错误吗?

3 个答案:

答案 0 :(得分:1)

您可以通过简单的方式完成此操作: 以下列方式在每个菜单中添加@yield:

<li class="@yield('menu_create_member') submenu-2"><a href="{{asset('member/create')}}">Create Member</a></li>

对于主菜单和子菜单,请尝试以下操作:

<li class="@yield('menu_member') submenu"> <a href="#"><i class="icon icon-list"></i> <span>Members</span></a>
  <ul>
   <li class="@yield('menu_create_member') submenu-2"><a href="{{asset('member/create')}}">Create Member</a></li>                        
   </ul>
 </li>

然后在加载

时扩展主模板(@extends('layouts.master'))后,在每个页面中添加此项
@section('menu_member','active')  // For main menu


@section('menu_create_member','active') // For sub menu

然后自动激活当前菜单

答案 1 :(得分:0)

喜欢这个。使用$。要加载jquery.Or,您还可以使用$(document).ready(function(){});

$(function() {
    var nav = document.getElementById('sidebar'),
        anchor = nav.getElementsByTagName('a'),
        current = window.location.pathname.split('/')[1];
        for (var i = 0; i < anchor.length; i++) {
        if(anchor[i].href == current) {
            anchor[i].className = "active";
        }
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="sidebar">   
    <ul>
        <li class="submenu"> <a href="#"><i class="icon icon-list"></i> <span>Members</span></a>
            <ul>
              <li class="submenu-2"><a href="{{asset('member/create')}}">Create Member</a></li>          
              <li class="submenu-2"><a href="{{asset('member')}}">Member List</a></li>               
            </ul>
        </li>
        <li class="submenu"> <a href="#"><i class="icon icon-list"></i> <span>Payroll</span></a>
            <ul>
                    <li class="submenu-2"><a href="{{asset('payroll/create')}}">Create Monthly Payroll</a></li>               
                    <li class="submenu-2"><a href="{{asset('managerPayroll')}}">Unapproved Monthly Payroll (Management)</a></li>                           
                    <li class="submenu-2"><a href="{{asset('nonManagerPayroll')}}">Unapproved Monthly Payroll (Employee)</a></li>
                    <li class="submenu-2"><a href="{{asset('printEmployeeAccount')}}">Print Employee Account</a></li>                     
            </ul>
        </li>
        <li class="submenu"> <a href="#"><i class="icon icon-list"></i> <span>Report</span></a>
            <ul>
                    <li class="submenu-2"><a href="{{asset('serachManagerReport')}}">Search Manager Report</a></li> 
                    <li class="submenu-2"><a href="{{asset('serachNonManagerReport')}}">Search Employee Report</a></li>
                    <li class="submenu-2"><a href="{{asset('salaryReport')}}">Salary Sheet </a></li> 
            </ul>
        </li>
        <li class="submenu-2"> <a href="{{asset('changePassword')}}"><i class="icon icon-list"></i> <span>Password Update</span></a>
        </li>
    </ul>

</div>

答案 2 :(得分:0)

通过将代码包装在括号内,您不会在内部调用函数。

(function() {
    var nav = document.getElementById('sidebar'),
        anchor = nav.getElementsByTagName('a'),
        current = window.location.pathname.split('/')[1];

    for (var i = 0; i < anchor.length; i++) {
        if(anchor[i].href == current) {
            anchor[i].className = "active";
            break;
        }
    }
}) (); // Add 2 parentheses to call the anonymous function

PS。 我还在for循环中添加了一个break,因为我猜你只需要在每页激活一个链接