我在这里使用带有css类数量的自定义引导程序导航栏。我在选择项目菜单上添加活动( menu__item - 当前)类时遇到了困难。我在底部添加了jquery代码
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">
<nav class="navbar navbar-default navbar-fixed-top">
<div class="navbar-header navbar-left">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="<?php echo ROOT;?>index.php"><img src="<?php echo ROOT;?>images/logob.png" width="70px" class="logo img-responsive"> </a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-right" id="bs-example-navbar-collapse-1">
<nav class="menu menu--iris">
<ul class="nav navbar-nav menu__list ">
<li class="menu__item menu__item--current "><a href="<?php echo ROOT;?>portfolio.php" class="menu__link">portfolios</a></li>
<li class="menu__item "><a href="<?php echo ROOT;?>team.php" class="menu__link">our team</a></li>
<li class="menu__item ">
<a href="#" class="menu__link">career</a></li>
<li class="menu__item"><a href="#" class="menu__link">Contact</a></li>
<li class="menu__item"><a href="#" class="menu__link">about</a></li>
<li class="menu__item"><a href="#" class="menu__link">members</a></li>
<li class="menu__item"><a href="#" class="menu__link">news</a></li>
</ul>
</nav>
</div>
</nav>
</div>
<script type="text/javascript">
$(document).ready(function () {
$('ul.nav > li').click(function () {
$('ul.nav > li').removeClass('menu__item--current');
$(this).addClass('menu__item--current');
});
});
</script>
&#13;
答案 0 :(得分:0)
它正常工作你需要像这样添加css
$(document).ready(function () {
$('ul.nav > li').click(function () {
$('ul.nav > li').removeClass('menu__item--current');
$(this).addClass('menu__item--current');
});
});
.menu__item--current a{
color:#ee5500!important;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">
<nav class="navbar navbar-default navbar-fixed-top">
<div class="navbar-header navbar-left">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="<?php echo ROOT;?>index.php"><img src="<?php echo ROOT;?>images/logob.png" width="70px" class="logo img-responsive"> </a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-right" id="bs-example-navbar-collapse-1">
<nav class="menu menu--iris">
<ul class="nav navbar-nav menu__list ">
<li class="menu__item menu__item--current "><a href="<?php echo ROOT;?>portfolio.php" class="menu__link">portfolios</a></li>
<li class="menu__item "><a href="<?php echo ROOT;?>team.php" class="menu__link">our team</a></li>
<li class="menu__item ">
<a href="#" class="menu__link">career</a></li>
<li class="menu__item"><a href="#" class="menu__link">Contact</a></li>
<li class="menu__item"><a href="#" class="menu__link">about</a></li>
<li class="menu__item"><a href="#" class="menu__link">members</a></li>
<li class="menu__item"><a href="#" class="menu__link">news</a></li>
</ul>
</nav>
</div>
</nav>
</div>
答案 1 :(得分:0)
$(document).ready(function () {
$(function(){
var current_page_URL = location.href;
$( "a" ).each(function() {
if ($(this).attr("href") !== "#") {
var target_URL = $(this).prop("href");
if (target_URL == current_page_URL) {
$('nav a').parents('li, ul').removeClass('menu__item--current');
$(this).parent('li').addClass('menu__item--current');
return false;
}
}
}); }); });
经过艰苦的努力后,找到了上面代码的解决方案。 谢谢大家试图帮助我。