js實(shí)現(xiàn)自定義路由
本文實(shí)現(xiàn)自定義路由,主要是事件hashchange的使用,然后根據(jù)我們的業(yè)務(wù)需求封裝。
首先實(shí)現(xiàn)一個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的時候,需要注意bind函數(shù)的使用,因?yàn)槲沂遣攘丝樱@才體會到$.proxy()。
上邊使用的時候可以使用兩種方法進(jìn)行注冊,但第二種是依賴第一種的。
方法一:
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>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
- Vue.js路由組件vue-router使用方法詳解
- angular.js之路由的選擇方法
- AngularJS 路由詳解和簡單實(shí)例
- AngularJS 路由和模板實(shí)例及路由地址簡化方法(必看)
- 全面解析JavaScript的Backbone.js框架中的Router路由
- 使用AngularJS對路由進(jìn)行安全性處理的方法
- JS實(shí)現(xiàn)簡單路由器功能的方法
- Angularjs制作簡單的路由功能demo
- director.js實(shí)現(xiàn)前端路由使用實(shí)例
- nodejs中實(shí)現(xiàn)路由功能
- 輕松創(chuàng)建nodejs服務(wù)器(4):路由
相關(guān)文章
Iframe自適應(yīng)高度絕對好使的代碼 兼容IE,遨游,火狐
Iframe自適應(yīng)高度絕對好使的代碼IE,遨游,火狐都兼容,需要的朋友可以參考下。2011-01-01
js 靜態(tài)動態(tài)成員 and 信息的封裝和隱藏
一下用面向?qū)ο蟮南嚓P(guān)概念來解釋js中的仿面向?qū)ο螅驗(yàn)閖s中不像其他語言,不存在面向?qū)ο笳Z言的相關(guān)特性2011-05-05
javascript中動態(tài)加載js文件多種解決辦法總結(jié)
這篇文章主要介紹了javascript中動態(tài)加載js文件多種解決辦法,有需要的朋友可以參考一下2013-11-11
JavaScript獲取radio選中值的幾種常用方法小結(jié)
這篇文章主要介紹了JavaScript獲取radio選中值的幾種常用方法,結(jié)合實(shí)例形式總結(jié)分析了javascript獲取radio選中值的常見實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2023-06-06

