单击将类添加到侧边栏父菜单和子菜单

时间:2018-11-12 09:55:08

标签: javascript jquery html css3

我想在点击时将类active添加到侧边栏父级和子级菜单中。我已经尝试过此代码,但无法正常工作。请检查我的代码,并建议我错过的内容和地点。

$("#sidebar-menu ul li.with_sub a.active").parents("li:last").children("a:first").addClass("active").trigger("click");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="slim-sidebar" id="sidebar-menu">
  <label class="sidebar-label">Navigation</label>
  <ul class="nav nav-sidebar">
    <li class="sidebar-nav-item with-sub">
      <a href="" class="sidebar-nav-link"><i class="icon ion-ios-home-outline"></i> Dashboard</a>
      <ul class="nav sidebar-nav-sub">
        <li class="nav-sub-item"><a href="menuother.html" class="nav-sub-link">Page 01</a></li>
        <li class="nav-sub-item"><a href="" class="nav-sub-link">Page 02</a></li>
        <li class="nav-sub-item"><a href="" class="nav-sub-link">Page 03</a></li>
        <li class="nav-sub-item"><a href="" class="nav-sub-link">Page 04</a></li>
        <li class="nav-sub-item"><a href="" class="nav-sub-link">Page 05</a></li>
      </ul>
    </li>
    <li class="sidebar-nav-item with-sub">
      <a href="" class="sidebar-nav-link"><i class="icon ion-ios-home-outline"></i> Menu</a>
      <ul class="nav sidebar-nav-sub">
        <li class="nav-sub-item"><a href="" class="nav-sub-link">Menu 01</a></li>
        <li class="nav-sub-item"><a href="" class="nav-sub-link">Menu 02</a></li>
        <li class="nav-sub-item"><a href="" class="nav-sub-link">Menu 03</a></li>
        <li class="nav-sub-item"><a href="" class="nav-sub-link">Menu 04</a></li>
        <li class="nav-sub-item"><a href="" class="nav-sub-link">Menu 05</a></li>
      </ul>
    </li>
  </ul>
</div>

1 个答案:

答案 0 :(得分:0)

检查以下代码,它可以正常工作

$("#sidebar-menu ul.nav li.with-sub li a").click(function() {
  $("#sidebar-menu ul.nav li.with-sub li a").removeClass("active");
  $(this).addClass('active');
  $("#sidebar-menu ul.nav li.with-sub a.sidebar-nav-link").removeClass("active");
  $(this).parent().parent().parent().find('a.sidebar-nav-link').addClass('active');
});
#sidebar-menu li a.active {
  color: black;
  font-weight:bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="slim-sidebar" id="sidebar-menu">
  <label class="sidebar-label">Navigation</label>
  <ul class="nav nav-sidebar">
    <li class="sidebar-nav-item with-sub">
      <a href="javaScript:void(0)" class="sidebar-nav-link"><i class="icon ion-ios-home-outline"></i> Dashboard</a>
      <ul class="nav sidebar-nav-sub">
        <li class="nav-sub-item"><a href="javaScript:void(0)" class="nav-sub-link">Page 01</a></li>
        <li class="nav-sub-item"><a href="javaScript:void(0)" class="nav-sub-link">Page 02</a></li>
        <li class="nav-sub-item"><a href="javaScript:void(0)" class="nav-sub-link">Page 03</a></li>
        <li class="nav-sub-item"><a href="javaScript:void(0)" class="nav-sub-link">Page 04</a></li>
        <li class="nav-sub-item"><a href="javaScript:void(0)" class="nav-sub-link">Page 05</a></li>
      </ul>
    </li>
    <li class="sidebar-nav-item with-sub">
      <a href="javaScript:void(0)" class="sidebar-nav-link"><i class="icon ion-ios-home-outline"></i> Menu</a>
      <ul class="nav sidebar-nav-sub">
        <li class="nav-sub-item"><a href="javaScript:void(0)" class="nav-sub-link">Menu 01</a></li>
        <li class="nav-sub-item"><a href="javaScript:void(0)" class="nav-sub-link">Menu 02</a></li>
        <li class="nav-sub-item"><a href="javaScript:void(0)" class="nav-sub-link">Menu 03</a></li>
        <li class="nav-sub-item"><a href="javaScript:void(0)" class="nav-sub-link">Menu 04</a></li>
        <li class="nav-sub-item"><a href="javaScript:void(0)" class="nav-sub-link">Menu 05</a></li>
      </ul>
    </li>
  </ul>
</div>