Menu 菜单上的主页链接未高亮显示
“我的菜单”显示单击时的活动链接,但“主页”链接除外()。它从不突出显示。 我试着到处玩,但我似乎想不出来。这是我用来突出显示链接的jquery代码Menu 菜单上的主页链接未高亮显示,menu,highlighting,breadcrumbs,Menu,Highlighting,Breadcrumbs,“我的菜单”显示单击时的活动链接,但“主页”链接除外()。它从不突出显示。 我试着到处玩,但我似乎想不出来。这是我用来突出显示链接的jquery代码 $(function(){ var path = location.pathname.substring(1); if ( path ) $('.nav a[href$="' + path + '"]').attr('class', 'active'); }); 我也有另一个菜单上,我想强调的父母的兄弟姐妹和我们的
$(function(){
var path = location.pathname.substring(1);
if ( path )
$('.nav a[href$="' + path + '"]').attr('class', 'active');
});
我也有另一个菜单上,我想强调的父母的兄弟姐妹和我们的产品在全球菜单上。这是products菜单的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在Firebug I get中,
}highlight.attr('class','active')行的highlight未定义
看起来您可能需要更正上面If语句周围的括号?我调试了您的Javascript。主页链接不会高亮显示,因为对于主页,location.pathname的计算结果为字符串“/”。因此,变量“path”被指定为空字符串。这意味着变量“highlight”未指定给
// path is assigned the empty string
var path = location.pathname.substring(1);
// evaluating to false
if (path) {
// we never get here
highlight = $('ul#accordion a[href$="' + path + '"]');
}
// getting a null pointer exception
highlight.attr('class', 'active');
我想出了如何让主页链接在菜单栏中突出显示(这是唯一一个不会在菜单栏上突出显示的链接)。以下是我所做的:
$(function(){
var pathname = location.pathname;
var path = pathname.substring(1);
if(path == "")
$('.nav a:first').addClass('active');
else (path)
$('.nav a[href$="' + path + '"]').attr('class', 'active');
});
谢谢你,戴夫。。我正在努力解决这个问题。这就是产品菜单-有没有关于为什么全局菜单上的主页链接不突出显示而其他所有内容都突出显示的提示?谢谢Stephen,这很有效-我甚至在第一个if语句上放了括号:)实际上,刚刚再次检查,即使在使用主页上更正的代码后,我也会出现突出显示未定义错误?我做错什么了吗?嗨。我回答中的代码实际上只是解释了为什么主页会出现错误:-)这不是问题的解决方案。要修复错误,您的代码需要考虑“path”是空字符串的情况。是的,这在我给您写信几分钟后才有意义。解决了吗:)