在导航中突出显示当前页面
我需要在左侧导航栏中突出显示当前页面。
导航必须通过 .shtml 从外部加载,包括:
<!--#include file="leftnav-menu.inc"-->
我的网址采用以下形式:
www.xxx.com/mission-ritic.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>
I need to highlight the current page in my left nav.
The nav has to be loaded externally via an .shtml include:
<!--#include file="leftnav-menu.inc"-->
My urls take the form of:
www.xxx.com/mission-critical.shtml
but sometimes just:
www.xxx.com/energy.shtml (eg one word no hyphen)
My nav lists it as 'Mission critical'
How can I highlight the ul li with "class=selected"? I've seen something like this:
$(function(){
var path = location.pathname.substring(1);
if ( path )
$('.leftmenuNav ul li a[@href$="' + path + '"]').attr('class', 'selected');
});
Can't quite get my head around the string splitting etc...
Sample nav bar:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,jQuery 语法有点错误。这应该有效:
另外,请确保 leftmenuNav 是正确的(上面的代码没有显示它)
Okay, the jQuery syntax was slightly wrong. This should work:
Also, make sure the leftmenuNav is right (your code above doesn't show it)