2

document.hrefと同じhrefを持つリンクにClassを追加することは可能かどうか疑問に思っていますか?

試してみましたが、運がありませんでした。

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

document.location.href の代わりに window.location.href を試しましたか?

于 2011-05-17T19:49:27.183 に答える