JS常⽤⽅法(获取Class、获取元素样式、事件监听、cookie、
ajax等)
var Util = {};
Util.byClass = function (oClass) {//全局获取
var tags = document.all ? document.all : ElementsByTagName('*');
var arr = [];
var reg = new RegExp("\\b" + oClass + "\\b");
for (var i = 0; i < tags.length; i++) {
if (st(tags[i].className)) {
arr.push(tags[i]);
}
}
return arr;
};
Util.byClass2 = function (parentID, oClass) {//局部获取
var parent = ElementById(parentID);
var tags = parent.all ? parent.all : ElementsByTagName('*');
var arr = [];
var reg = new RegExp("\\b" + oClass + "\\b");
for (var i = 0; i < tags.length; i++) {
if (st(tags[i].className)) {
arr.push(tags[i]);
selenium获取cookie
}
}
return arr;
};
if (obj.currentStyle) {
return obj.currentStyle[oStyle];
} else {
return getComputedStyle(obj, null)[oStyle];
}
};
Util.addEvent = function (obj, type , fn) {//添加事件监听
if (obj.addEventListener) {
obj.addEventListener(type, fn, false);
} else if (obj.attachEvent) {
obj.attachEvent('on' + type, fn);
} else {
obj['on' + type] = fn;
}
};
if (veEventListener) {
} else if (obj.detachEvent) {
obj.detachEvent('on' + type, fn);
} else {
obj['on' + type] = null;
}
};
var l = 0,
t = 0;
while (obj) {
l = l + obj.offsetLeft + obj.clientLeft;
t = t + obj.offsetTop + obj.clientTop;
obj = obj.offsetParent;
}
return {left: l, top, t};
}
Util.setCookie = function (key, val, days) {
var dates = new Date();
dates.Date() + days);
for (var i = 0; i < arr1.length; i++) {
var arr2 = arr1[i].split('=');
if (arr2[0] == key) {
return unescape(arr2[1]);
}
}
return false;
};
Util.setCookie(key, 'aaa', -1);
};
Util.ajax = function (url, successFn, failureFn) {
var xhr = null;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else {
xhr = new ActiveXObject('Microsoft.XMLHTTP'); }
xhr.open('get', url, true);
xhr.send(null);
if (adyState == 4) {
if (xhr.status == 400) {
sponsText);
} else {
if (failureFn) {
failureFn();
} else {
alert(xhr.status);
}
}
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论