我的主导航结构如下:
<li>
<a class="dropdown-toggle" href="about">
<?php echo nav_about ?>
</a>
<ul class="dropdown-menu">
<li>
<a href="<?php echo BASEURL ?>/about-us.html">
<?php echo nav_about_profile ?>
</a>
</li>
<li>
<a href="<?php echo BASEURL ?>/committee.html">
<?php echo nav_about_committee ?>
</a>
</li>
</ul>
</li>
当网址和链接匹配时,和相应项目的突出显示脚本:
/* highlight nav menu */
$(function(){
$('#topMain.nav-pills li a').each(function(index) {
if($.trim(this.href) == stripQueryStringAndHashFromPath(window.location.href)) {
$(this).closest('li').addClass('active');
}
});
});
function stripQueryStringAndHashFromPath(url) {
return url.split("?")[0].split("#")[0];
}
我可以将active
添加到其最近的超链接li,但我想找到一种方法将active
添加到最顶层的li中,结果如下:
<li class="active"> <-- I NEED THIS CLASS TO BE ADD ALSO
<a class="dropdown-toggle" href="about">
<?php echo nav_about ?>
</a>
<ul class="dropdown-menu">
<li class="active">
<a href="<?php echo BASEURL ?>/about-us.html">
<?php echo nav_about_profile ?>
</a>
</li>
<li>
<a href="<?php echo BASEURL ?>/committee.html">
<?php echo nav_about_committee ?>
</a>
</li>
</ul>
</li>
答案 0 :(得分:0)
试试这个:
$('.dropdown-menu li:has(a[href="'+opened_path+'"])').addClass('active')
.parent().closest('li')
.addClass('active');