0

我的菜单在单击时会显示活动链接,但主页链接 ( 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

4

3 回答 3

1

在Firebug中,我得到highlight is undefined}highlight.attr('class', 'active');看起来你可能需要更正它上面的If语句周围的括号?

于 2010-03-15T14:13:35.460 回答
1

我调试了你的 Javascript。主页链接没有突出显示,因为对于主页,location.pathname 被评估为字符串“/”。因此,变量“路径”被分配了空字符串。这意味着未分配变量“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');
于 2010-03-15T20:17:17.723 回答
0

我想出了如何让主页链接在菜单栏中突出显示(这是唯一不会在菜单栏中突出显示的链接)。这是我所做的:

 $(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');   
 });
于 2010-03-16T13:56:36.390 回答