vue的hash值原理也是table切換例項程式碼
阿新 • • 發佈:2020-12-16
我希望大家敲一遍
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> .pages>div{display: none;} </style> </head> <body> <p> <a href="#/" rel="external nofollow" >aaa</a> <a href="#/about" rel="external nofollow" >bbb</a> <a href="#/user" rel="external nofollow" >cccc</a> </p> <div class="pages"> <div id="home">首頁</div> <div id="about">關於我的頁面</div> <div class="user">使用者中心</div> </div> </body> <script type="text/javascript"> //hash 和頁面一一對應起來 //router 配置 var router = [ {path:"/",component:document.getElementById("home")},{path:"/about",component:document.getElementById("about")},{path:"/user",component:document.querySelector(".user")},] // 預設hash window.location.hash = "#/"; // 預設頁面 var currentView = router[0].component; currentView.style.display="block"; window.onhashchange=()=>{ //通過判斷hash切換div頁面 console.log(location.hash); //獲取hash值,不要井號 var hash = location.hash.slice(1); router.forEach(item=>{ if(item.path==hash){ //先隱藏之前顯示的頁面 currentView.style.display = "none"; // 顯示對應的元件 item.component.style.display = "block"; //重新設定當前顯示的頁面是哪個div currentView = item.component; } }) } </script> </html>
到此這篇關於vue的hash值原理也是table切換的文章就介紹到這了,更多相關vue hash原理內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!