我想在我的网站中将css
导航栏变为 include 。我在这篇文章中得到了一些答案:Twitter Bootstrap add active class to li我使用Jquery在我的导航栏中自动激活。
这是代码:
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" data-target="#navbarCollapse" data-toggle="collapse" class="navbar-toggle">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!-- Collection of nav links and other content for toggling -->
<div id="navbarCollapse" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li ><a href="../content/main.php#home">Home</a></li>
<li><a href="../content/news.php">News</a></li>
<li><a href="../content/contact.php">Contact</a></li>
<li class="dropdown" >
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"> Information <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="../content/info.php">Indoor</a></li>
<li><a href="../content/info.php#outdoor">Outdoor</a></li>
</ul>
</li>
<li class="dropdown" >
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Gallery<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="../content/photo.php">Photo</a></li>
<li><a href="../content/video.php">Video</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<script type="text/javascript">
var url = window.location;
// Will only work if string in href matches with location
$('ul.nav a[href="'+ url +'"]').parent().addClass('active');
// Will also work for relative and absolute hrefs
$('ul.nav a').filter(function() {
return this.href == url;
}).parent().addClass('active');
</script>
我的问题是当用户在子URL中时如何使下拉父项自动激活?感谢
注意:有一个下拉父级(图库部分),孩子们没有相同的网址/链接
答案 0 :(得分:0)
var url = window.location;
$('ul.nav li').removeClass('active'); //remove active class from all li
$('ul.nav a[href="'+ url +'"]').parents('li').addClass('active'); //add active class to current url's parents this will work in dropdown also