突出显示导航中的当前页面
-
18-09-2019 - |
题
我需要突出显示左导航中的当前页面。
必须通过.shtml包括:
<!--#include file="leftnav-menu.inc"-->
我的URL采用:
www.xxx.com/mission-critication.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>
解决方案
好的,jQuery语法有点错了。这应该有效:
$.ready(function()
{
var path = location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
if ( path )
$('.leftmenuNav ul li a[href="' + path + '"]').attr('class', 'selected');
});
另外,请确保LeftMenunav是正确的(上面的代码未显示)
不隶属于 StackOverflow