2010-02-05 68 views
0

我需要突出显示我左侧导航栏中的当前页面。突出显示导航中的当前页面

将NAV必须通过的.shtml外部加载包括:

<!--#include file="leftnav-menu.inc"--> 

我的网址采取以下形式:

www.xxx.com/mission-critical.shtml

但有时只是:

www.xxx.com/energy.shtml(例如,一个字无连字符)

我的导航列出它为'关键任务'

如何使用“class = selected”突出显示ul li?我见过这样的事情:

$(function(){ 
    var path = location.pathname.substring(1); 
    if (path) 
    $('.leftmenuNav ul li a[@href$="' + path + '"]').attr('class', 'selected'); 
});  

不能完全得到我的串绕分裂等头......

样品导航栏:

<ul> 
<li><a href="corporate-responsibility.shtml">Corporate responsibility</a></li> 
<li><a href="overview.shtml">Overview</a></li> 
<li><a href="governance.shtml">Governance</a></li> 
<li><a href="our-approach.shtml">Our approach</a></li> 
</ul> 
+0

这看起来是正确的。如果您需要更多帮助,请从导航栏中发布代码。 – 2010-02-05 11:53:38

+0

感谢马修,仍然没有工作..已发布导航 – 2010-02-05 12:01:00

回答

0

好吧,jQuery的语法有点不对。这应该工作:

$.ready(function() 
{ 
    var path = location.pathname.substring(location.pathname.lastIndexOf("/") + 1); 
    if (path) 
    $('.leftmenuNav ul li a[href="' + path + '"]').attr('class', 'selected'); 
}); 

此外,确保leftmenuNav是正确的(代码上面并没有表现出来)

+0

天才。这很有用。非常感谢您的帮助! – 2010-02-05 13:51:40

相关问题