jQuery 在子文件夹内的菜单上添加 class .active
jQuery add class .active on menu within subfolders
我找到了如何根据页面的 url 向菜单项添加 'active' class 的可靠解决方案:
jQuery(function(){
var page = window.location.pathname,
find = new RegExp(page == '/' ? window.location.origin + '/?jQuery' : page.replace(/\/jQuery/,''));
jQuery('nav a').each(function(){
if(find.test(this.href.replace(/\/jQuery/,''))){
jQuery(this).addClass('active');
}
});
});
然而,这不适用于子文件夹内的链接,我似乎无法找到一种方法来使其工作。我错过了什么?
jQuery(document).ready(function(jQuery){
var url = window.location.href;
jQuery('#nav a.level-top').filter(function() {
return this.href == url;
}).addClass('active');
jQuery('#nav a.active').closest('li').addClass('active');
});
这是我使用的,它适用于我的子文件夹中的菜单项
$(".menu-item").each(function() {
if (this.href == window.location.href) {
$(this).addClass("active");
}
});
我找到了如何根据页面的 url 向菜单项添加 'active' class 的可靠解决方案:
jQuery(function(){
var page = window.location.pathname,
find = new RegExp(page == '/' ? window.location.origin + '/?jQuery' : page.replace(/\/jQuery/,''));
jQuery('nav a').each(function(){
if(find.test(this.href.replace(/\/jQuery/,''))){
jQuery(this).addClass('active');
}
});
});
然而,这不适用于子文件夹内的链接,我似乎无法找到一种方法来使其工作。我错过了什么?
jQuery(document).ready(function(jQuery){
var url = window.location.href;
jQuery('#nav a.level-top').filter(function() {
return this.href == url;
}).addClass('active');
jQuery('#nav a.active').closest('li').addClass('active');
});
这是我使用的,它适用于我的子文件夹中的菜单项
$(".menu-item").each(function() {
if (this.href == window.location.href) {
$(this).addClass("active");
}
});