我想知道是否可以将 addClass 添加到与 document.href 具有相同 href 的链接?
我尝试过,但没有成功。
if ($("a").attr("href") == document.location.href) {
$(this).addClass("active");
}
这不可能吗?
最佳答案
$("a").filter(function() {
return this.href === document.location.href;
}).addClass("active");
应该可以。
关于javascript - jQuery:事件状态:将Class添加到包含document.href作为href的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6036256/