一些适用的js扩展方法

1.去掉数组中重复的值

Array.prototype.unique = function() {
	var data = this || [];
    var a = {}; //声明一个对象,javascript的对象可以当哈希表用
    for (var i = 0; i < data.length; i++) {
        a[data[i]] = true;  //设置标记,把数组的值当下标,这样就可以去掉重复的值
    }
    data.length = 0; 
    
    for (var i in a) { //遍历对象,把已标记的还原成数组
        this[data.length] = i; 
    } 
    return data;
};

var a = new Array("5","7","7");
alert(a);
alert(a.unique());

 

2.删除数组中指定的值

Array.prototype.remove = function(val) { 
    for(var i = 0; i < this.length; i++) { 
        if(this[i] == val) { 
        	for(var j = i; j < this.length - 1; j++)
            	this[j] = this[j + 1]; 
            this.length -= 1;
        }
    }    
}

 

3.在字符串的左边填充一些特定的字符

String.prototype.lpad = function(len, s) {
	var a = new Array(this);
	var n = (len - this.length);
	for ( var i = 0; i < n; i++) {
		a.unshift(s);
	}
	return a.join("");
}
 

 

4.在字符串的右边填充一些特定的字符

String.prototype.rpad = function(len, s) {
	var a = new Array(this);
	var n = (len - this.length);
	for ( var i = 0; i < n; i++) {
		a.push(s);
	}
	return a.join("");
}

 

 

5.获取星期几

function getWeek() {
	var a = new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六");
	var d = new Date().getDay();
	return a[d];
}

 

6.获取日期

function getDate() {
	var mydate = new Date();
	var y = mydate.getYear();
	var m = String(mydate.getMonth() + 1).lpad(2, "0");
	var d = String(mydate.getDate()).lpad(2, "0");
	var a = new Array(y, m, d);
	return a.join("-");
}

 

7.把字符串的首字母转化为大写

String.prototype.initialsToUpper = function() {
	return this.substring(0,1).toUpperCase().concat(this.substring(1));
};

 

8.判断一个字符串中是否包含某字符

String.prototype.contains = function(str) {
	return this.indexOf(str) > -1 ? true : false;
};

 

9.去掉字符串的空格

String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
};
   
String.prototype.lTrim = function() {
	return this.replace(/(^\s*)/g, "");
};
   
String.prototype.rTrim = function() { 
	return this.replace(/(\s*$)/g, "");
};

 

10.操作表单元素的几个js方法

//删除下拉框选项
function removeAllOptions(obj) {
	for (var k = obj.length - 1; k > 0; k--) {
		obj.remove(k);
	}
}

//添加下拉框选项
//其中data是action传过来的TreeMap值
function appendAllOptions(obj, data) {
	removeAllOptions(obj);
//	data = data.substr(1, data.length - 2);
	var arg = data.split(",");
	for (var i = 0; i < arg.length; i++) {
		var val = arg[i].split("=");
		obj.add(new Option(val[1], val[0]));
	}
}

//通过已选中checkbox的id获取当前行第n列的值,并组合成字符串返回
function getTDText(chkId, n) {
	var temp = "";
	var ids = chkId.split(",");
	for (var i = 0; i < ids.length; i++) {
		var tr = $(ids[i]).parentElement.parentElement;
		if (temp == "") {
			temp = tr.cells[n].innerText.trim();
		} else {
			temp += "," + tr.cells[n].innerText.trim();
		}
	}
	return temp;
}

//获取一组radio选中项的值
function getRadioValue(name) {
    var obj = document.getElementsByName(name);
    if (obj != null) {
        for(i = 0; i < obj.length; i++) {
            if (obj[i].checked) {
                return obj[i].value;            
            }
        }
    }
    return null;
}

//全选或取消全选
function chkAll(ele) {
	var objArray = document.getElementsByName("chk");
	for (var i = 0; i < objArray.length; i++) {
		objArray[i].checked = ele.checked;
	}
}

//获取选中的ID
function getChkVal() {
	var ids = "";
	var objArray = document.getElementsByName("chk");
	for (var i = 0; i < objArray.length; i++) {
		if (objArray[i].checked) {
			if (ids == "") {
				ids = objArray[i].id;
			} else {
				ids += "," + objArray[i].id;
			}
		}
	}
	return ids;
}

 

11.几个验证的js方法

//验证是否为合法的手机号码
String.prototype.isMobile = function() { 
	return /^(?:13\d|15[89])-?\d{5}(\d{3}|\*{3})$/.test(this.trim()); 
};

//验证是否为合法的电话号码或传真
String.prototype.isPhone = function() { 
	return /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/.test(this.trim()); 
};

//验证是否为合法的Email
String.prototype.isEmail = function() {
	return /^[a-zA-Z0-9_\-]{1,}@[a-zA-Z0-9_\-]{1,}\.[a-zA-Z0-9_\-.]{1,}$/.test(this.trim());
};

//验证是否为合法的邮编
String.prototype.isPost = function() {
	return /^\d{6}$/.test(this.trim());
};

//验证是否为合法的网址
String.prototype.isUrl = function() {
	var strRegex = "^((https|http|ftp|rtsp|mms)://)"  
	+ "(([0-9a-z_!~*’().&=+$%-]+: )?[0-9a-z_!~*’().&=+$%-]+@)?" //验证ftp的user@  
	+ "(([0-9]{1,3}\.){3}[0-9]{1,3}" // 验证IP形式的URL  
	+ "|" // 允许IP和DOMAIN(域名)  
	+ "([0-9a-z_!~*’()-]+\.)*" // 域名- www.  
	+ "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名  
	+ "[a-z]{2,6})" // 一级域名  
	+ "(:[0-9]{1,4})?" // 端口 	  
	var re = new RegExp(strRegex);  
	return re.test(this.trim());
};

 

12.清除非数字包含小数点

function clearNoNum(obj) {
			//先把非数字的都替换掉,除了数字和.
			obj.value = obj.value.replace(/[^\d.]/g,"");
			//必须保证第一个为数字而不是.
			obj.value = obj.value.replace(/^\./g,"");
			//保证只有出现一个.而没有多个.
			obj.value = obj.value.replace(/\.{2,}/g,".");
			//保证.只出现一次,而不能出现两次以上
			obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
		}

   onkeyup="clearNoNum(this)" (这个可以输入小数点)  或者 onkeyup="value=value.replace(/[^\d]/g,'')" (这个只能输入数字)

 

 

13.四舍五入并控制小数位数

Number(12345.67890).toFixed(2);

你可能感兴趣的:(JavaScript,prototype,J#,idea)