js跨浏览器事件工具:
//事件工具对象
var EventUtil = {};
//添加事件
EventUtil.addEvent = function(element,type,handle) {
if(element.addEventListener) { //dom事件
element.addEventListener(type,handle,false);
}
else if(element.attachEvent) {
element.attachEvent("on" + type,handle);
}
else {
element["on" + type] = handle;
}
}
//删除事件
EventUtil.removeEvent = function(element,type,handle) {
if(element.removeEventListener) { //dom事件
element.removeEventListener(type,handle,false);
}
else if(element.detachEvent) {
element.detachEvent("on" + type,handle);
}
else {
element["on" + type] = handle;
}
}
//得到event
EventUtil.getEvent = function(event) {
return event?event:window.event;
}
//得到当前事件拥有对象
EventUtil.getTarget = function(event) {
return event.targert || event.srcElement;
}
//阻止浏览器默认事件
EventUtil.preventDefault = function(event) {\
if(event.preventDefault) {
event.preventDefault();
}
else {
event.returnValue = false;
}
}
//阻止事件捕获或冒泡
EventUtil.stopPropagation = function(event) {
if(event.stopPropagation) {
event.stopPropagation();
}
else {
event.cancelBubble = false;
}
}