//替换所有字符 String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) { if (!RegExp.prototype.isPrototypeOf(reallyDo)) { return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith); } else { return this.replace(reallyDo, replaceWith); } } //存在重复元素 Array.prototype.iterate = function () { var flag = false; for (var i = 0, len = this.length; i < len; i++) { for (var j = 0; j < len; j++) { if (this[i] == this[j] && i != j) { flag = true; break; } } } return flag; }; 1.去掉数组中重复的值 Array.prototype.unique = function() { var data = []; var a = {}; //声明一个对象,javascript的对象可以当哈希表用 for (var i = 0; i < this.length; i++) { if(!a[this[i]]){ a[this[i]] = true; data[data.length] = this[i]; } } return data; }; var a = new Array("5","7","7"); alert(a); alert(a.unique()); 2.把字符串的首字母转化为大写 String.prototype.initialsToUpper = function() { return this.substring(0,1).toUpperCase().concat(this.substring(1)); }; 3.判断一个字符串中是否包含某字符 String.prototype.contains = function(str) { return this.indexOf(str) > -1 ? true : false; }; 4.去掉字符串的空格 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, ""); }; 5.操作表单元素的几个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; } 6.几个验证的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()); }; //传入一个回调,根据回调函数条件过滤数组 Array.prototype.filter = function(callback) { var rs = []; for ( var i = 0, length = this.length; i < length; i++ ) { if (callback(this[i], i)) { rs.push(this[i]); } } return rs; }; //找到返回所在索引,不存在返回-1 Array.prototype.index = function (el) { var i = 0; for (var i = 0, len = this.length; i < len; i++) { if (el == this[i]) { return i; } } return -1; };