<html>
<body>
<p>此示例使用 addEventListener() 方法将“hashchange”事件附加到 window 对象。</p>
<p>单击按钮将当前 URL 的锚点部分更改为#part5</p>
<button onclick="changePart()">试一试</button>
<p id="demo"></p>
<script>
// 使用 location.hash 属性更改锚点部分
function changePart() {
location.hash = "part5";
var x = location.hash;
document.getElementById("demo").innerHTML = "锚点部分现在是:" + x;
}
window.addEventListener("hashchange", myFunction);
function myFunction() {
alert("锚点部分发生了变化!");
}
</script>
</body>
</html>