| 1234567891011121314151617181920212223242526272829 |
- <!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 = 0;
- oBtn.onclick = function(){
- // 添加历史记录
- num++;
- history.pushState("h1"+num,"");
- }
- // 监听history变化
- window.onpopstate = function(e){
- console.log("history变化了",e.state);
- console.log(history.state);
- }
- // hash 和 history 区别
- // hash 是url的片段
- // history 是浏览器的历史记录
- </script>
- </body>
- </html>
|