Window: hashchange event
当 URL 的片段标识符更改时,将触发hashchange事件 (跟在#符号后面的 URL 部分,包括#符号)
| Bubbles | Yes |
|---|---|
| Cancelable | No |
| Interface | HashChangeEvent |
| Event handler | onhashchange |
示例
你可以在 addEventListener 方法中使用 hashchange 事件:
js
window.addEventListener('hashchange', function() {
console.log('The hash has changed!')
}, false);
或使用 onhashchange 事件处理程序属性:
js
function locationHashChanged() {
if (location.hash === '#cool-feature') {
console.log("You're visiting a cool feature!");
}
}
window.onhashchange = locationHashChanged;
规范
| Specification |
|---|
| HTML Standard # event-hashchange |
| HTML Standard # handler-window-onhashchange |
浏览器兼容性
BCD tables only load in the browser