日期:2014-05-16 浏览次数:20539 次
?
JS 通用常用方法comm类
?
?
/**
* 常用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() {
??? 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