gpt4 book ai didi

javascript - hashchange 触发 popstate

转载 作者:技术小花猫 更新时间:2023-10-29 12:32:25 30 4
gpt4 key购买 nike

Here's what I'm working with

代码:

<a href="#" onclick="window.onpopstate = function() { alert('pop'); };
return false; ">set up window.onpopstate
</a><br>
<a href="#somehash2">change hash</a>
<div onclick="alert(location.href);">show location.href</div>​

为什么单击 change hash 链接会触发 popstate,难道它不应该只在我单击 change hash 链接然后单击返回时触发吗?

最佳答案

window.onpopstate 触发的原因不是因为对哈希的更改。这是因为当你点击 anchor 标签时,历史已经被改变了。

来自 https://developer.mozilla.org/en/DOM/window.onpopstate :

A popstate event is dispatched to the window every time the active history entry changes. If the history entry being activated was created by a call to history.pushState() or was affected by a call to history.replaceState(), the popstate event's state property contains a copy of the history entry's state object.

关于javascript - hashchange 触发 popstate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9948510/

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