2

我想知道是否可以将类添加到与 document.href 具有相同href 的链接?

我试过了,但没有任何运气。

if ($("a").attr("href") == document.location.href) {
    $(this).addClass("active");
}

这不可能吗??

4

3 回答 3

6
$("a").filter(function() {
    return this.href === document.location.href;
}).addClass("active");

应该管用。

于 2011-05-17T19:48:49.747 回答
1
$("a[href=" + document.location.href + "]").addClass("active");

(未测试)

于 2011-05-17T19:50:30.820 回答
0

您是否尝试过 window.location.href 而不是 document.location.href?

于 2011-05-17T19:49:27.183 回答