添加基于子页面URL的活动导航类
Add Active Navigation Class Based on Child Page URL
我有一个基本的侧菜单结构,它是根据帖子主题动态生成的。使用下面的代码,我能够在查看单个主题页面时单独突出显示活动链接。然而,在列出所有主题的主页或索引页上,侧边导航中的每个锚元素都被赋予了一个'active'类。如何防止索引页sidenav锚接收"活动"类,同时在各个主题页面上保留"活动"类?
提前感谢任何可以提供一些见解的人…
侧导航结构:
<div class="sidenav">
<ul>
<li><a href="//example.com/customer/topic/government">Government</a></li>
<li><a href="//example.com/customer/topic/hospitality">Hospitality</a></li>
<li><a href="//example.com/customer/topic/industrial">Industrial</a></li>
</ul>
</div>
Javascript: $(function(){
var current = location.pathname;
$('.sidenav li a').each(function(){
var $this = $(this);
// if the current path is like this link, make it active
if($this.attr('href').indexOf(current) !== -1){
$this.addClass('active');
}
})
})
使用上述JS在各个主题页面上的当前输出:
<div class="sidenav">
<ul>
<li><a href="//example.com/customer/topic/government">Government</a></li>
<li><a href="//example.com/customer/topic/hospitality" class="active">Hospitality</a></li>
<li><a href="//example.com/customer/topic/industrial">Industrial</a></li>
</ul>
</div>
使用上述JS在主题索引页(/customer)上的当前输出
<div class="sidenav">
<ul>
<li><a href="//example.com/customer/topic/government" class="active">Government</a></li>
<li><a href="//example.com/customer/topic/hospitality" class="active">Hospitality</a></li>
<li><a href="//example.com/customer/topic/industrial" class="active">Industrial</a></li>
</ul>
</div>
尝试将最后一个键添加到当前可能是协议问题,这样您可以始终确保
$(function(){
var current = location.pathname;
current = current.substring(current.lastIndexOf('/'));
$('.sidenav li a').each(function(){
var $this = $(this);
// if the current path is like this link, make it active
if($this.attr('href').indexOf(current) !== -1){
$this.addClass('active');
}
});
});
相关文章:
- 如何在angular bootstrap ui中聚焦活动导航选项卡中的拳头输入字段
- 引导导航选项卡未突出显示活动选项卡
- 向服务器发送请求以获取活动引导导航药丸
- 引导导航活动状态不起作用
- Bootstrap/AngularJs:如何为导航类的活动选择设置粗体属性
- 使用CSS/JavaScript在活动页面上突出显示活动导航菜单项,无id
- 如何找到当前导航李"活动的”;在引导程序导航中
- 添加基于 URL 的活动导航类
- 如何将活动状态类动态更改(添加)到导航链接
- 如何在特定情况下突出显示活动导航选项卡
- 按部分更改活动导航的下划线
- 在活动导航项目下方显示箭头
- 重写一个Javascript函数,该函数可以选择在IE8中工作的活动导航选项卡
- 使用jquery和cookie单击时的活动导航栏按钮
- 页面滚动活动导航javascript
- 活动导航锚链接
- 添加基于子页面URL的活动导航类
- CSS代码如何改变活动导航按钮的颜色
- Jquery单个页面上的活动导航
- 如何在流星应用程序中按下后退按钮时正确设置活动导航选项卡