| 123456789101112131415161718192021222324252627 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <!-- hash -->
- <button id="btn">点击我</button>
- <script>
- // 获取元素
- var btn = document.getElementById("btn");
- // 绑定事件
- btn.onclick = function(){
- // 点击按钮后,改变hash值
- var ran = Math.random();
- location.hash = "#"+ran;
- }
- // 监听hash值变化 onhashchange
- window.onhashchange = function(){
- console.log(location.hash);
- }
- </script>
- </body>
- </html>
|