javascript - 检测导航至 <a name ="latest-topics"></a>

标签 javascript hash navigation location anchor

我需要检测 URL 导航到 anchor 的某些事件:

<a name="latest-topics"></a>

http://someserver.com/index.html#latest-topics

然后我想使用类似 onfocus 的事件, onclick

<a onclick="doSomething()" onfocus="doSomething()" name="latest-topics"></a>

有哪些选项可以在导航到此类 anchor 的精确事件中检测导航到该 anchor ?

或者我需要一个带有 setInterval 的计时器吗?持续轮询对 location.hash 的更改每隔几毫秒?

最佳答案

你可以这样做:

if(window.location.hash) {
 // do your check here.
}

关于javascript - 检测导航至 <a name ="latest-topics"></a>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12405092/

相关文章:

javascript - html5 listener loadeddata 不工作某些

javascript - 如何在文本输入中设置图标?

ruby - 有没有办法通过散列来初始化对象?

menu - Android支持:design NavigationView checked menu sub items

ruby-on-rails - 如何在不创建空模型的情况下创建 rails_admin 导航标签?

javascript - 滚动到 jquery 数组中的下一个元素

Javascript Engine V8 快速属性访问

arrays - 创建对哈希值的数组引用

java - 使用两个(或更多)对象作为 HashMap 键

纯 CSS 推送导航不会隐藏