菜单上的主页链接不突出显示
我的菜单在单击时显示活动链接,但主页链接除外 (http://www.obsia.com)。它永远不会被突出显示。 我试着玩玩,但我似乎无法弄清楚。这是我用来突出显示链接的 jquery 代码?
$(function(){
var path = location.pathname.substring(1);
if ( path )
$('.nav a[href$="' + path + '"]').attr('class', 'active');
});
我在产品页面上还有另一个菜单,我想在其中突出显示兄弟姐妹以及我们在全球菜单上的产品。这是产品菜单的 jquery 代码:
$(function() {
var pathname = location.pathname;
var highlight;
//highlight home
if(pathname == "")
highlight = $('ul#accordion > li:first > a:first');
else {
var path = pathname.substring(1);
if (path)
highlight = $('ul#accordion a[href$="' + path + '"]');
}highlight.attr('class', 'active');
// hide 2nd, 3rd, ... level menus
$('ul#accordion ul').hide();
// show child menu on click
$('ul#accordion > li > a.product_menu').click(function() {
//minor improvement
$(this).siblings('ul').toggle("slow");
return false;
});
//open to current group (highlighted link) by show all parent ul's
$('a.active').parents('ul').show();
$('a.active').parents('h2 a').css({'color':'#ff8833'});
//if you only have a 2 level deep navigation you could
//use this instead
//$('a.selected').parents("ul").eq(0).show();
}); });
我尝试添加这个:
$(this).parents('ul').addClass('active');
但这似乎不起作用?
有人有一个简单的方法来实现它吗? 任何帮助将不胜感激你们。
亲切的问候, G
My menu shows the active links when clicked on it except for the home link (http://www.obsia.com). It is never highlighted.
I tried playing around but I can't seem to figure it out. This is the jquery code I used to highlight the links?
$(function(){
var path = location.pathname.substring(1);
if ( path )
$('.nav a[href$="' + path + '"]').attr('class', 'active');
});
I also have another menu on the products pages where I would like to highlight the parents of the siblings and the our products on the global menu. This is the jquery code for the products menu:
$(function() {
var pathname = location.pathname;
var highlight;
//highlight home
if(pathname == "")
highlight = $('ul#accordion > li:first > a:first');
else {
var path = pathname.substring(1);
if (path)
highlight = $('ul#accordion a[href$="' + path + '"]');
}highlight.attr('class', 'active');
// hide 2nd, 3rd, ... level menus
$('ul#accordion ul').hide();
// show child menu on click
$('ul#accordion > li > a.product_menu').click(function() {
//minor improvement
$(this).siblings('ul').toggle("slow");
return false;
});
//open to current group (highlighted link) by show all parent ul's
$('a.active').parents('ul').show();
$('a.active').parents('h2 a').css({'color':'#ff8833'});
//if you only have a 2 level deep navigation you could
//use this instead
//$('a.selected').parents("ul").eq(0).show();
});
});
I tried adding this:
$(this).parents('ul').addClass('active');
but that does not seem to do the trick?
Does anybody have a simple way of accomplishing it?
Any help would be appreciated from you guys.
Kind Regards,
G
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我调试了你的Javascript。主页链接不会突出显示,因为对于主页,location.pathname 被计算为字符串“/”。因此,变量“path”被分配了空字符串。这意味着变量“highlight”未被分配。
I debugged your Javascript. The home link does not highlight because, for the home page, location.pathname is evaluated to the string "/". The variable 'path' is therefore assigned the empty string. This means that the variable 'highlight' is not assigned to.
在 Firebug 中,我在
}highlight.attr('class', 'active');
行上得到 highlight is undefined 看起来您可能需要更正 If 周围的括号上面的声明呢?In Firebug I get highlight is undefined on the line
}highlight.attr('class', 'active');
looks like you might need to correct the brackets around the If statement above it?我想出了如何让主页链接在菜单栏中突出显示(这是唯一不会在菜单栏上突出显示的链接)。这是我所做的:
I figured out how to get the home page link to highlight in the menu bar (That was the only link that would not highlight on the menu bar). Here is what I did: