JavaScript 中 onhashchange 事件有什么用?


锚点发生变化时,将会触发 onhashchange 事件。你可以尝试运行以下代码来了解如何在 JavaScript 中实现 onhashchange 事件。

示例

<!DOCTYPE html>
<html>
   <body onhashchange="newFunc">
      <button onclick="functionChange()">Click to change anchor</button>
      <script>
         function functionChange() {
            location.hash = "about";
            var hash = location.hash;
            document.write("The anchor part is now: " + hash);
         }
         // If the achor part is changed
         function newFunc() {
            alert("Anchor part changed!");
         }
      </script>
   </body>
</html>

更新日期: 2020-05-21

140 人浏览

开启你的 职业生涯

完成课程即可获得认证

开始
广告
© . All rights reserved.