1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| <body> <div class="box"> <div class="top"> 顶部通栏 </div> <div class="bottom"> <div class="slide"> <a href="#/pageA">pageA</a> <a href="#/pageB">pageB</a> <a href="#/pageC">pageC</a> <a href="#/pageD">pageD</a> </div> <div class="content router-view">内容区</div> </div> </div> <script> // 准备一些渲染内容, 后续会根据 Hash 值去展示 const templateA = `<div>templateA</div>` const templateB = `<div>templateB</div>` const templateC = `<div>templateC</div>` const templateD = `<div>templateD</div>` // 获取 Dom 节点 const rw = document.querySelector('.router-view') // 监听 hash 值的变化,hash值发生变化时会触发该事件 window.onhashchange = function () { const { hash } = window.location; // 根据当前的 Hash 值, 决定渲染哪一段内容 if (hash === '#/pageA') { rw.innerHTML = templateA } else if (hash === '#/pageB') { rw.innerHTML = templateB } else if (hash === '#/pageC') { rw.innerHTML = templateC } else if (hash === '#/pageD') { rw.innerHTML = templateD } } </script> </body>
|