1. <a href=""> 实现跳转
    2. window.hashchange 监听实现跳转对应路由

      1. <!-- 1、定义视图,(点击)改变哈希地址 -->
      2. <ul>
      3. <li><a href="#/index">首页</a></li>
      4. <li><a href="#/list">列表页</a></li>
      5. <!-- 注意:href填的是hash地址,这里的 #/ 不要去掉 ,为的是不改变域名(地址栏中#前面的为域名)-->
      6. </ul>
      7. <div id="routerView"></div>
      8. <script>
      9. // 2、定义路由
      10. let louter = {
      11. //路由名称 视图页面
      12. "#/index": "./index.html",
      13. "#/list": "./list.html"
      14. }
      15. //3、检测哈希地址的改变(事件监听)
      16. window.addEventListener('hashchange',()=>{
      17. //location.hash可以获取哈希地址
      18. console.log("location.hash为: ", location.hash)
      19. // 可以看出location.hash就是我们的键,我们可以通过 louter[键名] 来获取值,即获取视图页面
      20. let url = louter[location.hash]
      21. // console.log(url)
      22. // 加载对应视图
      23. // 在<div id="routerView"></div>中load加载需要的视图页面
      24. $("#routerView").load(url)
      25. })
      26. </script>