事件操作對象: var EventUrl = { //添加事件 addHandler: function (element, type, handler) { if (element.addEventListener) { element.addEventListener(type, handler ...
事件操作對象:
var EventUtil= {
//添加事件
addHandler: function (element, type, handler) {
if (element.addEventListener) {
element.addEventListener(type, handler, false);
}else if (element.attachEvent) { //IE
element.attachEvent("on" + type, handler, false);
} else {
element["on" + type] = handler;
}
},
//移除對象
removeHandler: function (element, type, handler) {
if (element.removeEventListener) {
element.removeEventListener(type, handler, false);
} else if (element.detachEvent) { //IE
element.detachEvent("on" + type, handler, false);
} else {
element["on" + type] = null;
}
},
//獲取事件對象
getEvent: function (event) {
return event ? event : window.event;
},
//獲取目標對象
getTarget: function (event) {
return event.target || event.srcElement;
},
//取消預設事件
preventDefault: function (event) {
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue;
}
},
//停止冒泡
stopPropagation: function (event) {
if (event.stopPropagation) {
event.stopPropagation();
} else {
event.cancelBubble = true;
}
}
};