js實(shí)現(xiàn)自定義路由
本文實(shí)現(xiàn)自定義路由,主要是事件hashchange的使用,然后根據(jù)我們的業(yè)務(wù)需求封裝。
首先實(shí)現(xiàn)一個(gè)router的類,并實(shí)例化。
function _router(config){
this.config = config ? config : {};
}
_router.prototype = {
event:function(str,callback){
var events = str.split(' ');
for (var i in events) window.addEventListener(events[i],callback,false);
},
init: function() {
this.event('load hashchange',this.refresh.bind(this));
return this;
},
refresh: function() {
this.currentUrl = location.hash.slice(1) || '/';
this.config[this.currentUrl]();
},
route: function(path,callback){
this.config[path] = callback || function(){};
}
}
function router (config){
return new _router(config).init();
}
上邊唯一需要注意的是,在使用addEventListener的時(shí)候,需要注意bind函數(shù)的使用,因?yàn)槲沂遣攘丝?,這才體會(huì)到$.proxy()。
上邊使用的時(shí)候可以使用兩種方法進(jìn)行注冊(cè),但第二種是依賴第一種的。
方法一:
var Router = router({
'/' : function(){content.style.backgroundColor = 'white';},
'/1': function(){content.style.backgroundColor = 'blue';},
'/2': function(){content.style.backgroundColor = 'green';}
})
方法二:
Router.route('/3',function(){ content.style.backgroundColor = 'yellow'; })
完整代碼:
<html>
<head>
<title></title>
</head>
<body>
<ul>
<li><a href="#/1">/1: blue</a></li>
<li><a href="#/2">/2: green</a></li>
<li><a href="#/3">/3: yellow</a></li>
</ul>
<script>
var content = document.querySelector('body');
function _router(config){
this.config = config ? config : {};
}
_router.prototype = {
event:function(str,callback){
var events = str.split(' ');
for (var i in events) window.addEventListener(events[i],callback,false);
},
init: function() {
this.event('load hashchange',this.refresh.bind(this));
return this;
},
refresh: function() {
this.currentUrl = location.hash.slice(1) || '/';
this.config[this.currentUrl]();
},
route: function(path,callback){
this.config[path] = callback || function(){};
}
}
function router (config){
return new _router(config).init();
}
var Router = router({
'/' : function(){content.style.backgroundColor = 'white';},
'/1': function(){content.style.backgroundColor = 'blue';},
'/2': function(){content.style.backgroundColor = 'green';}
})
Router.route('/3',function(){
content.style.backgroundColor = 'yellow';
})
</script>
</body>
</html>
<script>
</script>
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
- Vue.js路由組件vue-router使用方法詳解
- angular.js之路由的選擇方法
- AngularJS 路由詳解和簡(jiǎn)單實(shí)例
- AngularJS 路由和模板實(shí)例及路由地址簡(jiǎn)化方法(必看)
- 全面解析JavaScript的Backbone.js框架中的Router路由
- 使用AngularJS對(duì)路由進(jìn)行安全性處理的方法
- JS實(shí)現(xiàn)簡(jiǎn)單路由器功能的方法
- Angularjs制作簡(jiǎn)單的路由功能demo
- director.js實(shí)現(xiàn)前端路由使用實(shí)例
- nodejs中實(shí)現(xiàn)路由功能
- 輕松創(chuàng)建nodejs服務(wù)器(4):路由
相關(guān)文章
bootstrap-Treeview實(shí)現(xiàn)級(jí)聯(lián)勾選
這篇文章主要為大家詳細(xì)介紹了bootstrap-Treeview實(shí)現(xiàn)級(jí)聯(lián)勾選,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
如何使用require.context實(shí)現(xiàn)優(yōu)雅的預(yù)加載
這篇文章主要介紹了使用require.context實(shí)現(xiàn)優(yōu)雅的預(yù)加載?,需要的朋友可以參考下2023-05-05
干貨分享:讓你分分鐘學(xué)會(huì)javascript閉包
干貨分享:讓你分分鐘學(xué)會(huì)javascript閉包,如何才能快速學(xué)會(huì)javascript閉包,本文為大家揭曉2015-12-12
論壇特效代碼收集(落伍轉(zhuǎn)發(fā)-不錯(cuò))
論壇特效代碼收集(落伍轉(zhuǎn)發(fā)-不錯(cuò))...2006-12-12
利用javascript實(shí)現(xiàn)web頁(yè)面中指定區(qū)域打印
將需要打印的課程表的table放入div標(biāo)簽中,然后指定出需要打印的區(qū)域,最后調(diào)用window.print打印指定內(nèi)容2013-10-10
JavaScript兼容性總結(jié)之獲取非行間樣式案例
這篇文章主要介紹了JavaScript兼容性總結(jié)之獲取非行間樣式的相關(guān)資料,需要的朋友可以參考下2016-08-08

