javascript代码实用方法实现Javascript代码实用方法实现针对现在大家平时开发中,都会写一些重复性的js处理代码,今天总结了几个比较常用的方法实现。获取get请求参数、去字符串空格。1、获取get请求中的参数
Js代码functiongetPara(para){if(location.href.indexOf("?")==-1){//没有参数,则Donothing.returnnull;}else{//取得GET请求?号后面的字符串varurlQuery=location.href.split("?");if(urlQuery[1].indexOf("&")==-1){//只有一个参数if(urlQuery[1].indexOf("=")==-1){//没有等号,没有参数,则Donothingreturnnull;}else{varkeyValue=urlQuery[1].split("=");varkey=keyValue[0];varvalue=keyValue[1];if(key==para){returnvalue;}}}else{//解析参数varurlTerms=urlQuery[1].split("&");for(vari=0;i<urlTerms.length;i++){varkeyValue=urlTerms[i].split("=");varkey=keyValue[0];varvalue=keyValue[1];if(key==para){returnvalue;}}}}returnnull;} 2、 //本函数用于去掉字符串左边的空格Js代码functionleftTrim(str){if(str.charAt(0)==""){str=str.slice(1);str=leftTrim(str);}returnstr;} 3、 //本函数用于去掉字符串右边的空格Js代码functionrightTrim(str){if(str.length-1>=0&&str.charAt(str.length-1)==""){str=str.slice(0,str.length-1);str=rightTrim(str);}returnstr;} 4、 //将时间转换成固定格式输出Js代码/***将时间转换成固定格式输出*newDate().toFormat('yyyy-MM-ddHH:mm:ss');*newDate().toFormat('yyyy/MM/ddhh:mm:ss');*只支持关键字(yyyy、MM、dd、HH、hh、mm、ss)HH:表示24小时,hh表示12小时*/Date.PRototype.toFormatString=function(format){varformatstr=format;if(format!=null&&format!=""){//设置年if(formatstr.indexOf("yyyy")>=0){formatstr=formatstr.replace("yyyy",this.getFullYear());}//设置月if(formatstr.indexOf("MM")>=0){varmonth=this.getMonth()+1;if(month<10){month="0"+month;}formatstr=formatstr.replace("MM",month);}//设置日if(formatstr.indexOf("dd")>=0){varday=this.getDay();if(day<10){day="0"+day;}formatstr=formatstr.replace("dd",day);}//设置时-24小时varhours=this.getHours();if(formatstr.indexOf("HH")>=0){if(month<10){month="0"+month;}formatstr=formatstr.replace("HH",hours);}//设置时-12小时if(formatstr.indexOf("hh")>=0){if(hours>12){hours=hours-12;}if(hours<10){hours="0"+hours;}formatstr=formatstr.replace("hh",hours);}//设置分if(formatstr.indexOf("mm")>=0){varminute=this.getMinutes();if(minute<10){minute="0"+minute;}formatstr=formatstr.replace("mm",minute);}//设置秒if(formatstr.indexOf("ss")>=0){varsecond=this.getSeconds();if(second<10){second="0"+second;}formatstr=formatstr.replace("ss",second);}}returnformatstr;}