国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

JS實現(xiàn)前端路由功能示例【原生路由】

瀏覽:79日期:2024-05-06 13:14:30

本文實例講述了JS實現(xiàn)前端路由功能。分享給大家供大家參考,具體如下:

路由就是根據(jù)不同的 url 地址展示不同的內(nèi)容或頁面,早期路由的概念是在后端出現(xiàn)的,通過服務器端渲染后返回頁面,隨著頁面越來越復雜,服務器端壓力越來越大。后來ajax異步刷新的出現(xiàn)使得前端也可以對url進行管理,此時,前端路由就出現(xiàn)了。

單頁面就是有前端路由來實現(xiàn)的,也就是說網(wǎng)站只有一個頁面,點擊導航會顯示不同的內(nèi)容,對應的url也在發(fā)生改變。在這個過程中,js會實時檢測url的變化,從而改變顯示的內(nèi)容。

JS實現(xiàn)前端路由功能示例【原生路由】

路由實現(xiàn)的原理:window綁定了監(jiān)聽函數(shù),當url的hash值發(fā)生變化的時候會觸發(fā)hashchange回調(diào),在回調(diào)中進行不同的操作,馬上刷新頁面,從而顯示不同的頁面。

下面是一個前端路由的簡單實現(xiàn):通過路由實現(xiàn)url的切換、頁面內(nèi)容的改變。

index.html

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <meta http-equiv='X-UA-Compatible' content='ie=edge'> <title>前端路由測試</title> <script src='https://www.jq22.com/jquery/jquery-3.3.1.js'></script> <style> *{margin:0;padding: 0; } .content{width: 500px;height: 300px;margin-top: 30px;margin:20px auto 0; } #click_btn{width: 500px;height: 50px;margin:100px auto 0; } #click_btn a{display: block;background: #333;color: #fff;text-decoration: none;line-height: 50px;text-align: center;float: left;margin-right: 15px;padding: 0px 15px; } #click_btn a:hover{background: #666; } </style> </head><body><div id='click_btn'> <a href='http://www.intensediesel.com/bcjs/14619.html#/one' rel='external nofollow' >第一個頁面</a> <a href='http://www.intensediesel.com/bcjs/14619.html#/two' rel='external nofollow' >第二個頁面</a> <a href='http://www.intensediesel.com/bcjs/14619.html#/three' rel='external nofollow' >第三個頁面</a></div><div class='content'></div> <script src='http://www.intensediesel.com/bcjs/router.js'></script><script src='http://www.intensediesel.com/bcjs/test.js'></script> </body></html>

router.js

//構造函數(shù)function Router() { this.routes = {}; this.currentUrl = ’’;}Router.prototype.route = function(path, callback) { this.routes[path] = callback || function(){};//給不同的hash設置不同的回調(diào)函數(shù)};Router.prototype.refresh = function() { console.log(location.hash.slice(1));//獲取到相應的hash值 this.currentUrl = location.hash.slice(1) || ’/’;//如果存在hash值則獲取到,否則設置hash值為/ // console.log(this.currentUrl); if(this.currentUrl&&this.currentUrl!=’/’){ this.routes[this.currentUrl]();//根據(jù)當前的hash值來調(diào)用相對應的回調(diào)函數(shù) } };Router.prototype.init = function() { window.addEventListener(’load’, this.refresh.bind(this), false); window.addEventListener(’hashchange’, this.refresh.bind(this), false);}//給window對象掛載屬性window.Router = new Router();window.Router.init();

test.js

Router.route(’/one’, function () { $('.content').html('<p>路由就是根據(jù)不同的 url 地址展示不同的內(nèi)容或頁面,早期路由的概念是在后端出現(xiàn)的,通過服務器端渲染后返回頁面,隨著頁面越來越復雜,服務器端壓力越來越大。后來ajax異步刷新的出現(xiàn)使得前端也可以對url進行管理,此時,前端路由就出現(xiàn)了。</p>');});Router.route(’/two’, function () { $('.content').html('<h3>單頁面就是有前端路由來實現(xiàn)的,也就是說網(wǎng)站只有一個頁面,點擊導航會顯示不同的內(nèi)容,對應的url也在發(fā)生改變。在這個過程中,js會實時檢測url的變化,從而改變顯示的內(nèi)容。</h3>');});Router.route(’/three’, function () { $('.content').html('<img src=’https://upload-images.jianshu.io/upload_images/12890819-f8665293cc8d0dcf.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp’ width=’500’/>');});

注意:router.js要在test.js之前進行調(diào)用,不然會先加載test.js從而找不到,出現(xiàn)router.js未被定義。

上面router對象實現(xiàn)主要提供了三個方法

1.init監(jiān)聽瀏覽器url的hash值更新事件。

2.route存儲路由更新時的回調(diào)到回調(diào)數(shù)組routes中,回掉函數(shù)將負責對頁面進行更新。

3.refresh執(zhí)行當前url的回調(diào)函數(shù),更新頁面。

感興趣的朋友可以使用在線HTML/CSS/JavaScript前端代碼調(diào)試運行工具:http://tools.jb51.net/code/WebCodeRun測試上述代碼運行效果。

更多關于JavaScript相關內(nèi)容可查看本站專題:《JavaScript切換特效與技巧總結》、《JavaScript遍歷算法與技巧總結》、《JavaScript查找算法技巧總結》、《JavaScript動畫特效與技巧匯總》、《JavaScript錯誤與調(diào)試技巧總結》、《JavaScript數(shù)據(jù)結構與算法技巧總結》及《JavaScript數(shù)學運算用法總結》

希望本文所述對大家JavaScript程序設計有所幫助。

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 莲花县| 蒙自县| 蕲春县| 梁平县| 卢氏县| 皮山县| 五寨县| 门源| 于都县| 沅陵县| 隆安县| 九龙县| 抚州市| 宝应县| 高碑店市| 昭觉县| 沛县| 云阳县| 西安市| 东安县| 岑溪市| 延长县| 徐汇区| 菏泽市| 石阡县| 宣恩县| 游戏| 古交市| 黄龙县| 光泽县| 浏阳市| 城市| 沙湾县| 自贡市| 衢州市| 屏山县| 广灵县| 新宁县| 鹤峰县| 古蔺县| 江门市|