国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

JS實現簡單路由器功能的方法

2024-05-06 16:20:55
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了JS實現簡單路由器功能的方法,基于javascript模擬簡單路由編碼的相關技巧,需要的朋友可以參考下

本文實例講述了JS實現簡單路由器功能的方法。分享給大家供大家參考。具體實現方法如下:

 

 
  1. var wawa = {}; 
  2. wawa.Router = function(){ 
  3. function Router(){ 
  4. Router.prototype.setup = function(routemap, defaultFunc){ 
  5. var that = this, rule, func; 
  6. this.routemap = []; 
  7. this.defaultFunc = defaultFunc; 
  8. for (var rule in routemap) { 
  9. if (!routemap.hasOwnProperty(rule)) continue
  10. that.routemap.push({ 
  11. rule: new RegExp(rule, 'i'), 
  12. func: routemap[rule] 
  13. });  
  14. }; 
  15. Router.prototype.start = function(){ 
  16. console.log(window.location.hash); 
  17. var hash = location.hash, route, matchResult; 
  18. for (var routeIndex in this.routemap){ 
  19. route = this.routemap[routeIndex]; 
  20. matchResult = hash.match(route.rule); 
  21. if (matchResult){ 
  22. route.func.apply(window, matchResult.slice(1)); 
  23. return;  
  24. this.defaultFunc(); 
  25. }; 
  26. return Router; 
  27. }(); 
  28. var router = new wawa.Router(); 
  29. router.setup({ 
  30. '#/list/(.*)/(.*)'function(cate, id){ 
  31. console.log('list', cate, id); 
  32. }, 
  33. '#/show/(.*)'function(id){ 
  34. console.log('show', id);  
  35. }, function(){ 
  36. console.log('default router'); 
  37. }); 
  38. router.start(); 

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 临泽县| 宿松县| 黄陵县| 云林县| 东城区| 绍兴县| 九龙坡区| 长丰县| 禹州市| 榕江县| 蕲春县| 九江市| 万源市| 承德县| 平塘县| 开阳县| 河北区| 湾仔区| 彰武县| 寿阳县| 乌拉特中旗| 尉犁县| 南木林县| 克东县| 双柏县| 宜丰县| 阜阳市| 忻州市| 巫山县| 德惠市| 内江市| 习水县| 太仓市| 通城县| 鄂尔多斯市| 瓮安县| 固始县| 化德县| 陇南市| 清丰县| 罗甸县|