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.