Javascript 将类活动添加到引导导航
目前,在bootstrap3 nav下拉列表中,等等Javascript 将类活动添加到引导导航,javascript,jquery,html,css,twitter-bootstrap,Javascript,Jquery,Html,Css,Twitter Bootstrap,目前,在bootstrap3 nav下拉列表中,等等 <ul class="nav navbar-nav navbar-right"> <li><a href="#">Link</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" ari
<ul class="nav navbar-nav navbar-right">
<li><a href="#">Link</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><a href="#">Here's My Fancy St. Patties Day link!</a></li>
<li><a href="#">Another link</a></li>
<li><a href="#">Something else entirely here</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</li>
</ul>
然后,当一个菜单项位于所述页面上时,它会突出显示相应的菜单项
我的问题是,如何将类添加到容器“”(li>ul>li,等等)?我一直在堆积。parent()标签试图找到它,运气不好
编辑:
我认为类似这样的东西会将活动类添加到容器li class='dropdown':
$('a[href="' + this.location.pathname + '"]').parent().addClass('active');
$('li a[href="' + this.location.pathname + '"]').parent().eq(1).addClass('active');
…使用.parent().eq(),但仍然是nada 移动到最近的位置。下拉列表
$('a[href="' + this.location.pathname + '"]').closest('.dropdown').addClass('active');
移动到最近的位置。下拉列表
$('a[href="' + this.location.pathname + '"]').closest('.dropdown').addClass('active');
您可以使用选择器:
$('a[href="' + this.location.pathname + '"]').closest('.dropdown')
您可以使用选择器:
$('a[href="' + this.location.pathname + '"]').closest('.dropdown')
我想出来了:
$('a[href="' + this.location.pathname + '"]').parent().addClass('active');//get the menu item
$('a[href="' + this.location.pathname + '"]').parents('li').last().addClass('active'); //get the parent
谢谢大家。。用.eq(1)遍历它似乎也找不到它。。我需要调查“为什么” 找到了答案:
$('a[href="' + this.location.pathname + '"]').parent().addClass('active');//get the menu item
$('a[href="' + this.location.pathname + '"]').parents('li').last().addClass('active'); //get the parent
$('a[href="' + this.location.pathname + '"]').parent().addClass('active');
$('a[href="' + this.location.pathname + '"]').parent().siblings().removeClass('active');
谢谢大家。。用.eq(1)遍历它似乎也找不到它。。我需要调查“为什么”
$('a[href="' + this.location.pathname + '"]').parent().addClass('active');
$('a[href="' + this.location.pathname + '"]').parent().siblings().removeClass('active');
这样也可以从其他同级中删除类
这样也可以从其他同级中删除类。要在哪个元素上添加活动的?在
a
或li
?.parentsUntil()
上,如果您只是将.active
添加到所有元素中,则应该帮助您解决问题。除了链接多个.parent()
方法之外,您还应该使用.closest()
-$('a[href=“+this.location.pathname+”).closest('dropdown')).addClass(“活动”)
在该容器a
或li
?.parentsUntil()
上,如果您只是将.active
添加到所有元素中,则应该帮助您解决问题。除了链接多个.parent()
方法之外,您还应该使用.closest()
-$('a[href=“+this.location.pathname+”).closest('dropdown')).addClass(“活动”)
在该容器