我想知道是否可以将类添加到与 document.href 具有相同href 的链接?
我试过了,但没有任何运气。
if ($("a").attr("href") == document.location.href) {
$(this).addClass("active");
}
这不可能吗??
我想知道是否可以将类添加到与 document.href 具有相同href 的链接?
我试过了,但没有任何运气。
if ($("a").attr("href") == document.location.href) {
$(this).addClass("active");
}
这不可能吗??
$("a").filter(function() {
return this.href === document.location.href;
}).addClass("active");
应该管用。
$("a[href=" + document.location.href + "]").addClass("active");
(未测试)
您是否尝试过 window.location.href 而不是 document.location.href?