当URL的片段标识符更改时,将触发hashchange事件 (跟在#符号后面的URL部分,包括#符号)
Bubbles | Yes |
---|---|
Cancelable | No |
Interface | HashChangeEvent |
Event handler | onhashchange |
示例
你可以在 addEventListener
方法中使用 hashchange
事件:
window.addEventListener('hashchange', function() {
console.log('The hash has changed!')
}, false);
或使用 onhashchange
事件处理程序属性:
function locationHashChanged() {
if (location.hash === '#cool-feature') {
console.log("You're visiting a cool feature!");
}
}
window.onhashchange = locationHashChanged;
规范
规范 | 状态 | 注释 |
---|---|---|
HTML Living Standard hashchange |
Living Standard | Initial definition |
浏览器兼容性
BCD tables only load in the browser
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.