菜单上的主页链接不突出显示

发布于 2024-08-25 08:27:58 字数 1554 浏览 9 评论 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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夢归不見 2024-09-01 08:27:58

我调试了你的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');

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.

// 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');
清醇 2024-09-01 08:27:58

在 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?

本宫微胖 2024-09-01 08:27:58

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

 $(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');   
 });

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:

 $(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');   
 });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文