1. Array 数组的清空:
Array.prototype.clear = function() {
this.length = 0;
}
2. Array数组的contains实现:
Array.prototype.contains = function(element) {
var index = -1;
for (var i = 0; i < this.length; i++) {
if (this[i] == element) {
return i;
}
}
return index;
}
3. 字符串转换为Date类型:
//datePattern: 日期格式 e.g. yyyy-MM-dd, dd/MM/yyyy, MM/dd/yyyy...
//dateSeparator:日期格式的分隔符 e.g. -,/... 更好点的是能从日期格式里自动分析出分隔符,查找非数字的应该就是
String.prototype.parseToDate = function(datePattern, dateSeparator) {
var datePatternArr = datePattern.split(dateSeparator);
var dateStrArr = this.split(dateSeparator);
var year, month, date;
try {
for (var i = 0; i < datePatternArr.length; i++) {
if (datePatternArr[i] == "yyyy" || datePatternArr[i] == "yy") {
year = dateStrArr[i];
}
else if (datePatternArr[i] == "MM" || datePatternArr[i] == "M") {
month = dateStrArr[i];
}
else if (datePatternArr[i] == "dd" || datePatternArr[i] == "d") {
date = dateStrArr[i];
}
}
var parsedDate = new Date();
parsedDate.setFullYear(year);
parsedDate.setMonth(month - 1);
parsedDate.setDate(date);
return parsedDate;
}
catch (e) {
return this;
}
}
4.日期比较:
var s1 = txtEndDateValue.parseToDate(pattern, separator);
var s2 = txtDateValue.parseToDate(pattern, separator);
var dataRange = document.getElementById('<%= hfDateRange.ClientID %>').value;
if (dataRange == "") {
dataRange = 60;
}
var time = s1.getTime() - s2.getTime();
var days = parseInt(time / (1000 * 60 * 60 * 24));
if (days > dataRange) {
alert("Data range must less than "+dataRange+" days.");
needInitUserPicDiv = false;
return;
}
5.float 取值
//f:需要操作的数值
//dec:需要保留的小数位
function tofloat(f, dec) {
if (dec < 0) return f;
result = parseInt(f) + (dec == 0 ? "" : ".");
f -= parseInt(f);
if (f == 0)
for (var i = 0; i < dec; i++) result += '0';
else {
for (var i = 0; i < dec; i++) f *= 10;
result += parseInt(Math.round(f));
}
return result;
}
//判断是否数字可以包括小数点
function isNumber(input)
{
//var pattern=/^[0-9]+$/;
var pattern = /^[0-9]+(.[0-9]+)?$/;
return pattern.exec(input);
}
6.字符串StartWith函数
String.prototype.startWith = function(str) {
if (str == null || str == "" || this.length == 0 || str.length > this.length)
return false;
if (this.substr(0, str.length) == str)
return true;
else
return false;
return true;
}
7. 捕获键盘输入(IE and FF)
function keyPress(sender, args) {
if (args.keyCode == 13) {
alter("Enter key pressed");
return false;
}
}
<asp:TextBox ID="txtDescription" runat="server" οnkeypress="return keyPress(this, event)"></asp:TextBox>
8.根据class取对象
function getElementsByClassName(classname, node) {
if (!node) node = document.getElementsByTagName("body")[0];
var a = []; var re = new RegExp('//b' + classname + '//b');
var els = node.getElementsByTagName("*");
for (var i = 0, j = els.length; i < j; i++)
if (re.test(els[i].className)) a.push(els[i]);
return a;
}
var getElementsByClassName = function (className, tag, elm){
if (document.getElementsByClassName) {
getElementsByClassName = function (className, tag, elm) {
elm = elm || document;
var elements = elm.getElementsByClassName(className),
nodeName = (tag)? new RegExp("//b" + tag + "//b", "i") : null,
returnElements = [],
current;
for(var i=0, il=elements.length; i<il; i+=1){
current = elements[i];
if(!nodeName || nodeName.test(current.nodeName)) {
returnElements.push(current);
}
}
return returnElements;
};
}
else if (document.evaluate) {
getElementsByClassName = function (className, tag, elm) {
tag = tag || "*";
elm = elm || document;
var classes = className.split(" "),
classesToCheck = "",
xhtmlNamespace = "http://www.w3.org/1999/xhtml",
namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
returnElements = [],
elements,
node;
for(var j=0, jl=classes.length; j<jl; j+=1){
classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
}
try {
elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
}
catch (e) {
elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
}
while ((node = elements.iterateNext())) {
returnElements.push(node);
}
return returnElements;
};
}
else {
getElementsByClassName = function (className, tag, elm) {
tag = tag || "*";
elm = elm || document;
var classes = className.split(" "),
classesToCheck = [],
elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
current,
returnElements = [],
match;
for(var k=0, kl=classes.length; k<kl; k+=1){
classesToCheck.push(new RegExp("(^|//s)" + classes[k] + "(//s|$)"));
}
for(var l=0, ll=elements.length; l<ll; l+=1){
current = elements[l];
match = false;
for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
match = classesToCheck[m].test(current.className);
if (!match) {
break;
}
}
if (match) {
returnElements.push(current);
}
}
return returnElements;
};
}
return getElementsByClassName(className, tag, elm);
};
//设置区域的高度e.g. BoxHeights.equalise('half_1','half_2'); half_1为class名
var BoxHeights = {
maxh: 0,
boxes: Array(),
num: 0,
op_test: false,
equalise: function() {
this.num = arguments.length;
for (var i=0;i<this.num;i++) if (!$(arguments[i])) return;
this.boxes = arguments;
this.maxheight();
for (var i=0;i<this.num;i++) $(arguments[i]).style.height = this.maxh+"px";
},
maxheight: function() {
var heights = new Array();
for (var i=0;i<this.num;i++) {
if (navigator.userAgent.toLowerCase().indexOf('opera') == -1) {
heights.push($(this.boxes[i]).scrollHeight);
} else {
heights.push($(this.boxes[i]).offsetHeight);
}
}
heights.sort(this.sortNumeric);
this.maxh = heights[this.num-1];
},
sortNumeric: function(f,s) {
return f-s;
}
}
9.asp.net中全选/全不选CheckBoxList控件
function funCheckAllDevice(checkAll) {
var field = document.getElementById('<%=cblResource.ClientID %>');
if (field!= null) {
field = field.getElementsByTagName("INPUT");
for (var i = 0; i < field.length; i++) {
field[i].checked = checkAll;
}
}
}
10.asp.net中取所有选中的CheckBoxList item
var devs = new Array();
var field= document.getElementById('<%=cblResource.ClientID %>');
if (field!= null) {
field = field.getElementsByTagName("INPUT");
for (var i = 0; i < field.length; i++) {
if (field[i].checked) {
var labelArray = field[i].parentNode.getElementsByTagName('label');
if (labelArray.length > 0) {
devs.push(labelArray[0].innerText);
}
}
}
}