| 12345678910111213141516171819202122232425262728293031 |
- <!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>
- <button>按钮</button>
- <script>
- var oBtn = document.querySelector("button");
- var num = 1;
- oBtn.onclick = function(){
- // window.location是BOM对象下的 地址对象 可以获取当前页面的地址信息
- // hash 是地址栏中 # 后面的内容
- num++;
- // 向hash添加内容 添加历史记录
- window.location.hash = "code"+num;
- }
- // 捕捉历史记录变化事件
- // 当hash值发生变化时 会触发hashchange事件
- window.onhashchange = function(){
- // console.log("历史记录变化了");
- // 可以获取当前hash值
- var hash = window.location.hash;
- console.log(hash);
- }
- </script>
- </body>
- </html>
|