在JavaScript中,onhashchange事件的用途是什么?

原创
ithorizon 5个月前 (10-19) 阅读数 46 #HTML

当锚点部分改变时,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>

以上就是在JavaScript中,onhashchange事件的用途是什么?的详细内容,更多请关注IT视界其它相关文章!



热门