【发布时间】:2020-01-30 14:07:25
【问题描述】:
我正在尝试根据活动 URL 向 li 添加一个活动类, 这是我得到的,不幸的是,当
<li class="scroll"><a href="index.php">Home</a></li>
它会在以下情况下工作:
<li class="scroll"><a href="">Home</a></li>
jQuery 是:
jQuery(function($) {
var path = window.location.href;
if ( path == '' ) {
path = 'index.php';
}
$('ul li a').each(function() {
if (this.href === path) {
$(this).parent().addClass('is-current');
}
});
});
谢谢!
【问题讨论】:
-
console.log(path,path===this.href) -
$(function() { var path = window.location.pathname; if ( path === '' ) path = 'index.php'; $('ul li a[href='+path+']').parent().addClass('is-current'); });
标签: jquery