我知道这个问题在这个论坛上已经有很多了,但我在这里遇到了一个问题,尽管谷歌搜索了几个小时。我希望当我选择/单击子菜单时,父li
使用Javascript / Jquery获取类.actives
。谢谢你的帮助 :)
这是我的代码:
<nav class="navbar navbar-default navbar-static-top">
<div class="container-fluid container">
<div class="row m04m">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#main_nav">
<span class="bars">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</span>
<span class="btn-text">Select Page</span>
</button>
</div>
<div class="collapse navbar-collapse" id="main_nav">
<ul class="nav navbar-nav">
<li class="active"><a href="index-2.html">Home</a></li>
<li class=" dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">ABOUT</a>
<ul class="dropdown-menu" role="menu">
<li><a href="about-us.html">About Company</a></li>
<li><a href="about-plasma.html">About Jump</a></li>
</ul>
</li>
<li><a href="services.html">LINK</a></li>
</ul>
</div>
</div>
</div>
</nav> <!--Main Nav-->
这是我的css
.actives{background-color:#174069;}
感谢您的帮助
答案 0 :(得分:1)
此示例代码将帮助您将其修改为您的要求
$('#menu').find('li').click(function(){
//removing the previous selected menu state
$('#menu').find('li.active').removeClass('active');
//is this element from the second level menu?
if($(this).closest('ul').hasClass('secondLevel')){
$(this).parents('li').addClass('active');
//this is a parent element
}else{
$(this).addClass('active');
}
});
#menu li.active{
background: #ccddff;
}
演示示例代码: http://jsfiddle.net/L94N6/4/