国产探花免费观看_亚洲丰满少妇自慰呻吟_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程序設計有所幫助。

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

圖片精選

主站蜘蛛池模板: 龙南县| 榆林市| 皮山县| 柳林县| 成都市| 饶阳县| 望江县| 洱源县| 德清县| 宁国市| 岳池县| 郴州市| 溆浦县| 洛南县| 衡山县| 洪泽县| 博湖县| 鸡泽县| 美姑县| 潍坊市| 林甸县| 古蔺县| 突泉县| 临朐县| 开封县| 聂拉木县| 高碑店市| 柳林县| 山西省| 东光县| 清水河县| 土默特右旗| 宜都市| 汨罗市| 镇宁| 永康市| 西林县| 昌宁县| 金塔县| 神木县| 西林县|