首页 > 其他 > 详细

历史记录

时间:2018-03-20 12:23:56      阅读:191      评论:0      收藏:0      [点我收藏+]
 1     <a  class="api a">a.html</a>
 2     <a  class="api b">b.html</a>
 3     <script>
 4       // 注册路由
 5       document.querySelectorAll(.api).forEach(item=>{
 6           item.addEventListener(click,(e)=>{
 7               e.preventDefault();
 8               let link=item.textContent
 9               window.history.pushState({name:api},link,link);
10           },false)
11       });
12       // 监听路由
13       window.addEventListener(popstate,(e)=>{
14           console.log({
15               location:location.href,
16               state:e.state
17           });
18       },false)
19     </script>
20     <hr>
21   <h3>Hash</h3>
22     <a  class="hash c">c.html</a>
23     <a  class="hash d">d.html</a>
24     <script>
25       document.querySelectorAll(.hash).forEach(item=>{
26           item.addEventListener(click,(e)=>{
27               e.preventDefault();
28               let link=item.textContent;
29               location.hash=link;
30           },false)
31       });
32       window.addEventListener(hashchange,(e)=>{
33           console.log({
34               location:location.href,
35               hash:location.hash
36           })
37       })
38     </script>

 

历史记录

原文:https://www.cnblogs.com/yangguoe/p/8608265.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!