javascript - jQuery:事件状态:将Class添加到包含document.href作为href的链接

标签 javascript jquery location-href

我想知道是否可以将 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/

相关文章:

javascript - 从未调用过 Mongodb 插入回调

javascript - 在 Canvas 中清除和填充矩形与背景颜色之间的区别

javascript - 对所有 div 使用一个 onchange javascript 函数

javascript - 错误: 'this' object is incorrect; In JQuery click event - Unexpected Behavior on click

javascript - 使用 jQuery 选择特定 href 的 anchor 标记

javascript - window.location.hash 始终返回前一个哈希值,而不是当前哈希值

javascript - Internet Explorer 父窗口上的动态 CSS

javascript - JSON.Stringify 替换函数无法获取键值

javascript - 在 JQuery 循环中的 Ajax 请求之前发出警报

Jquery:使整个 block 可点击,同时保留在新选项卡/窗口中打开的能力