JavaScript 通用常用方法comm类

openkk 9年前
     包括:    <span><span class="comment">去掉前后空格</span> 、</span>    <span><span class="comment">去掉左空格、</span></span>    <span><span class="comment">只留下数字(0123456789)、</span></span>    <span><span class="comment">将时间转换成固定格式输出</span>等。</span>    <span> <pre class="brush:javascript; toolbar: true; auto-links: false;">/** *  常用JS 通用类 *  author:panfu */   /** * 去掉前后空格 * " dd ".trim(); == "dd" */ String.prototype.trim = function() {     return this.replace(/(^\s*)|(\s*$)/g, ""); }  /** * 去掉左空格 * " dd".leftTrim(); == "dd" */ String.prototype.leftTrim = function() {     return this.replace(/(^\s*)/g, ""); }  /** * 去掉右空格 * "dd ".rightTrim(); == "dd" */ String.prototype.rightTrim = function() {     return this.replace(/(\s*$)/g, "");  } /** * 只留下数字(0123456789) * "dd 09".toNumber(); == "" * onkeyup="change_number(this)"  * onafterpaste="change_number(this)" */ String.prototype.toNumber = function() { &nbsp;&nbsp;&nbsp; return this.replace(/\D/g, "");   /** * 删除数组指定下标或指定对象 * arr.remove(2);//删除下标为2的对象(从0开始计算) * arr.remove(str);//删除指定对象 */ Array.prototype.remove=function(obj){  for(var i =0;i <this.length;i++){   var temp = this[i];   if(!isNaN(obj)){    temp=i;   }   if(temp == obj){    for(var j = i;j <this.length;j++){     this[j]=this[j+1];    }    this.length = this.length-1;   }   } } /** * 将时间转换成固定格式输出 * new Date().toFormat('yyyy-MM-dd HH:mm:ss'); * new Date().toFormat('yyyy/MM/dd hh:mm:ss'); * 只支持关键字(yyyy、MM、dd、HH、hh、mm、ss)HH:表示24小时,hh表示12小时 */ Date.prototype.toFormatString=function(format){     var formatstr = format;     if(format != null && format != ""){         //设置年         if(formatstr.indexOf("yyyy") >=0 ){             formatstr = formatstr.replace("yyyy",this.getFullYear());         }         //设置月         if(formatstr.indexOf("MM") >=0 ){             var month = this.getMonth() + 1;             if(month < 10){                 month = "0" + month;             }             formatstr = formatstr.replace("MM",month);         }         //设置日         if(formatstr.indexOf("dd") >=0 ){             var day = this.getDay();             if(day < 10){                 day = "0" + day;             }             formatstr = formatstr.replace("dd",day);         }         //设置时 - 24小时         var hours = 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 ){             var minute = this.getMinutes();             if(minute < 10){                 minute = "0" + minute;             }             formatstr = formatstr.replace("mm",minute);         }         //设置秒         if(formatstr.indexOf("ss") >=0 ){             var second = this.getSeconds();             if(second < 10){                 second = "0" + second;             }             formatstr = formatstr.replace("ss",second);         }     }     return formatstr; }  //离开该页面时,提示! window.onbeforeunload = function() {     if (commn.IsSearch == true) {         return "\n警告!~  \n操作正在执行中,确认需要继续?\n";     } }  //commn对象 var commn ={     IsSearch:false,//是否正在查询数据     InputDisabled: function(eid) {//按钮点击后,按钮不可用 例如:window.setTimeout("commn.InputDisabled('#bt_submit,#bt_back')", 1);         commn.IsSearch =true;         jQuery(eid).attr("disabled","disabled");     },     DateDiffDay:function (beginDate,endDate){//获取两个时间的天数差         //beginDate、endDate 格式:2011-8-25         var arrDate = new Array();         //设置开始时间         arrDate = beginDate.split("-");         beginDate = new Date(arrDate[1] + "/" + arrDate[2] + "/" + arrDate[0]);//默认格式:8/25/2011         //设置结束时间         arrDate = endDate.split("-");         endDate = new Date(arrDate[1] + "/" + arrDate[2] + "/" + arrDate[0]);//默认格式:8/25/2011         var iDays = parseInt(Math.abs((beginDate-endDate)/1000/60/60/24));//转换天,默认毫秒         return iDays;     },     DateTimeIsFomart:function (val){//验证时分秒格式是否正确12:00:25         //判断时间位数是否正确         if(val.length == 8){             var val_r = val.replace(/\D/g,'');//只取数字             if(val_r.length == 6){//判读位置是否正确                 var val_s = val.split(":");//按:分成数组                 if(val_s.length == 3){//如果数组正确                     var v0 = parseInt(val_s[0]);                     var v1 = parseInt(val_s[1]);                     var v2 = parseInt(val_s[2]);                     // 当时分秒的值 处于正常范围时,返回true                     if(v0 != null && (v0 >= 0 && v0 <= 23) &&                         v1 != null && (v1 >= 0 && v1 <= 59) &&                         v2 != null && (v2 >= 0 && v2 <= 59)                      ){                        return true;                     }                 }             }         }         return false;     } }</pre>转自:<a href="/misc/goto?guid=4959500036390157596" target="_blank">http://panfuy.iteye.com/blog/1329312</a><br /> </span>