日期:2014-05-16 浏览次数:20394 次
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; } } }