将类添加到包含指向当前页面的菜单链接的 li
Add class to the li that contains the menu link to the current page
我有下面的菜单,我想将类active
添加到包含用户当前正在查看的页面链接的<li>
。
<ul class="sidebar-menu">
<li class="treeview"><a href="#"><i class="fa fa-dashboard"></i> <span>Text</span></a></li>
<li class="treeview"><a href="#"><i class="fa fa-th"></i><span>Subtext 1</span><i class="fa fa-angle-left pull-right"></i></a>
<ul class=" treeview-menu">
<li><a href="#"><i class="fa fa-circle-o"></i> Text 1</a></li>
<li><a href="#"><i class="fa fa-circle-o"></i> Text 2</a></li>
</ul>
</li>
</ul>
我尝试了这个jQuery代码,但它对我不起作用:
$(function(){
var url = window.location.pathname,
urlRegExp = new RegExp(url.replace(/'/$/,'') + "$"); // create regexp to match current url pathname and remove trailing slash if present as it could collide with the link in navigation in case trailing slash wasn't present there
// now grab every link from the navigation
$('.treeview a').each(function(){
// and test its normalized href against the url pathname regexp
if(urlRegExp.test(this.href.replace(/'/$/,''))){
$(this).addClass('active treeview-menu');
}
});
});
为了完成这项工作,我对你的代码做了一些更改:
- 我没有将类添加到
a
,而是按照您的指定将其添加到li
。 - 我没有使用正则表达式,而是使用自定义函数来删除前导斜杠和尾随斜杠。(不确定这样做是否绝对必要,但它不会造成伤害。
- 您需要将类
treeview
添加到所有li
中,如果这是您要在选择器中使用的类。
我没有使用 - 而是使用
pathname
属性来获取链接的 URL,因为它需要与函数顶部的location.pathname
匹配。当然,您也可以与主机名进行比较,关键是您需要将苹果与苹果而不是橙子进行比较。
href
属性,$(function(){
//Use the function removeSlash to clean up URL instead of using RegExp.
var url = removeSlash(window.location.pathname);
//Loop through the treeview elements (the li) instead of the a.
$('.treeview').each(function(){
//Find the href of the a in the li.
var link = $(this).find('a')[0];
//Use the link we just found instead of this.
//Also, instead of href, use pathname. That will give just the path, not the host.
if(url == removeSlash(link.pathname)) {
//This referes to the li, so we can keep this line of code as it is.
$(this).addClass('active');
}
});
});
function removeSlash(url) {
//Remove leading slash, if there is one.
if(url.charAt(0) == '/') url = url.substring(1);
//Remove trailing slash, if there is one.
if(url.charAt(url.length-1) == '/') url = url.substring(0, url.length-1);
//Return the result.
return url;
}
工作 JSFiddle。请注意,在很多情况下,这可能不起作用。例如,我不确定如果当前URL http://example.com/site.php?x=4
并且链接的href
/site.php
会发生什么。您应该能够通过修改 removeSlash
函数来实现处理此类情况的方法。
试试下面的方法。它查找以当前 URL 作为其href
的链接,查找其父元素,并将类应用于父元素。
$(document).ready(function() {
$('a[href="' + this.location.pathname + '"]').parent().addClass('active treeview-menu');
});
相关文章:
- 我的下拉菜单中的链接不起作用
- 绝对链接/相对链接均未获胜't在菜单中工作
- Javascript+动态菜单+当前链接样式(CSS)+基于PHP的网站
- 当主导航链接悬停时,在侧菜单中显示链接
- 如何在每个下拉菜单项的每个类别下输出链接
- 下拉菜单在我进入链接之前消失了
- 如何分配数据切换=“;下拉菜单”;用于wordpress中的父链接
- 用这个jQuery动画菜单悬停超链接
- 每个链接的菜单下方的进度条
- Php菜单查询数据库并显示文本/链接
- 在表格最后一行(链接)中显示菜单,并在单击菜单项时显示模式窗口
- 单击手风琴菜单外的链接时折叠jQuery手风琴
- 在阻止默认操作时启用菜单的链接
- jQuery UI链接选择菜单.单击按钮时窗体将重置
- 如何在单击子链接时停止垂直菜单的手风琴
- 当屏幕尺寸较小时,可使用Javascript向移动菜单添加链接.不再有效
- 当子菜单的父级或包含链接处于活动状态时,如何告知子菜单可见
- 如何在 jQuery 中为链接我的“活动”滚动菜单创建例外
- 下拉菜单-链接不起作用
- 重叠的选项卡/下拉菜单-链接不起作用-CSS或JS问题