gpt4 book ai didi

javascript - 在点击事件/函数期间获取 URL 哈希

转载 作者:行者123 更新时间:2023-11-29 21:14:25 27 4
gpt4 key购买 nike

在 jQuery click 事件函数中,我知道 click 事件是在底层事件完成(并且 URL 已更新)之前触发的...因此您可以控制该事件。这意味着链接的哈希值还不能通过 window.location.hash

e.target.hash 是否比进入 <a> 元素本身获取哈希值更好?或者通过 onclick 事件传递必要的变量? “更好”定义为跨浏览器兼容性或更容易出错。

例如:

<a class="accountLink" href="#account/123456">Account 123456</a>
$('.accountLink').click(function(e){
console.log(e.target.hash);
console.log(window.location.hash);
});

日志:

#account/123456
(blank on initial click)

https://jsfiddle.net/r1xpsut1/1/

最佳答案

根据您的要求,windowonhashchange 事件听起来更合适:

window.onhashchange = function() {
alert(window.location.hash);
}

Updated fiddle

关于javascript - 在点击事件/函数期间获取 URL 哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40047091/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com