jQuery构建路由
生活随笔
收集整理的這篇文章主要介紹了
jQuery构建路由
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
buildMUrl:function( path ,params){var url = "/m" + path;var _paramUrl = '';if( params ){//處理參數(shù)_paramUrl = Object.keys(params).map(function(k) {return [encodeURIComponent(k), encodeURIComponent(params[k])].join("=");}).join('&');_paramUrl = "?"+_paramUrl;}//url到文件的//_paramUrl為參數(shù)部分的return url + _paramUrl},
分析:
url構(gòu)建好了文件部分
Object.keys(params)獲取參數(shù)的所有的key
map函數(shù)進(jìn)行數(shù)組map處理:key與value之間用=連接? ?各個(gè)參數(shù)之間用&連接!
進(jìn)行拼接:url(到文件部分)+param(參數(shù)部分)? 最后執(zhí)行join
<!DOCTYPE html> <html> <body> <ul id="ul"><li>1</li><li>2</li><li>3</li><li>4</li> </ul> </body> </html> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script type="text/javascript">$(document).ready(function(){var arr = ['a', 'b', 'c'];result = Object.keys(arr).map(function(k) {console.log(k);console.log([encodeURIComponent(k), encodeURIComponent(arr[k])].join("="));return [encodeURIComponent(k), encodeURIComponent(arr[k])].join("=");}).join('&');console.log(result);//0=a&1=b&2=c// 每次循環(huán)出現(xiàn)一組數(shù)字 最后進(jìn)行拼接join// 0// 0=a// // 1// 1=b// // 2// 2=c}) </script>整體代碼
;var common_ops = {init:function(){this.eventBind();this.setIconLight();},eventBind:function(){},setIconLight:function(){var pathname = window.location.pathname;var nav_name = null;if( pathname.indexOf("/m/default") > -1 || pathname == "/m" || pathname == "/m/" ){nav_name = "default";}if( pathname.indexOf("/m/product") > -1 ){nav_name = "product";}if( pathname.indexOf("/m/user") > -1 ){nav_name = "user";}if( nav_name == null ){return;}$(".footer_fixed ."+nav_name).addClass("aon");},buildMUrl:function( path ,params){var url = "/m" + path;var _paramUrl = '';if( params ){_paramUrl = Object.keys(params).map(function(k) {return [encodeURIComponent(k), encodeURIComponent(params[k])].join("=");}).join('&');_paramUrl = "?"+_paramUrl;}return url + _paramUrl},buildWwwUrl:function( path ,params){var url = path;var _paramUrl = '';if( params ){_paramUrl = Object.keys(params).map(function(k) {return [encodeURIComponent(k), encodeURIComponent(params[k])].join("=");}).join('&');_paramUrl = "?"+_paramUrl;}return url + _paramUrl},buildPicUrl:function( bucket,img_key ){var upload_config = eval( '(' + $(".hidden_layout_warp input[name=upload_config]").val() +')' );var domain = "http://" + window.location.hostname;return domain + upload_config[ bucket ] + "/" + img_key;},notlogin:function( referer ){if ( ! referer) {alert('授權(quán)過(guò)期,系統(tǒng)將引導(dǎo)您重新授權(quán)');referer = location.pathname + location.search;}window.location.href = this.buildMUrl("/user/bind",{ referer:referer });} };$(document).ready( function() {common_ops.init(); });// 對(duì)Date的擴(kuò)展,將 Date 轉(zhuǎn)化為指定格式的String // 月(M)、日(d)、小時(shí)(h)、分(m)、秒(s)、季度(q) 可以用 1-2 個(gè)占位符, // 年(y)可以用 1-4 個(gè)占位符,毫秒(S)只能用 1 個(gè)占位符(是 1-3 位的數(shù)字) // 例子: // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 // (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18 Date.prototype.Format = function(fmt) { //author: meizzvar o = {"M+" : this.getMonth()+1, //月份"d+" : this.getDate(), //日"h+" : this.getHours(), //小時(shí)"m+" : this.getMinutes(), //分"s+" : this.getSeconds(), //秒"q+" : Math.floor((this.getMonth()+3)/3), //季度"S" : this.getMilliseconds() //毫秒};if(/(y+)/.test(fmt))fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));for(var k in o)if(new RegExp("("+ k +")").test(fmt))fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));return fmt; };?
總結(jié)
以上是生活随笔為你收集整理的jQuery构建路由的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 借呗逾期跟客服协商分期 需要说明自己
- 下一篇: 积分兑换提现额度要还吗