javascript 浏览器兼容事件处理类EventUtil

 1 var EventUtil = {
 2     addHandler: function (element, type, handler) {
 3         if (element.addEventListener) {
 4             element.addEventListener(type, handler, false);
 5         } else if (element.attachEvent) {
 6             element.attachEvent("on" + type, handler);
 7         } else {
 8             element["on" + type] = handler;
 9         }
10     },
11     removeHandler: function (element, type, handler) {
12         if (element.removeEventListener) {
13             element.removeEventListener(type, handler, false);
14         } else if (element.detachEvent) {
15             element.detachEvent("on" + type, handler);
16         } else {
17             element["on" + type] = null;
18         }
19     },
20 
21     getEvent: function (event) {
22         return event ? event : window.event;
23     },
24     getTarget: function (event) {
25         return event.target || event.srcElement;
26     },
27     preventDefault: function (event) {
28         if (event.preventDefault) {
29             event.preventDefault();
30         } else {
31             event.returnValue = false;
32         }
33     },
34     stopPropagation: function (event) {
35         if (event.stopPropagation) {
36             event.stopPropagation();
37         } else {
38             event.cancelBubbles = true;
39         }
40     },
41     getRelatedTarget: function (event) {
42         if (event.relatedTarger) {
43             return event.relatedTarget;
44         } else if (event.toElement) {
45             return event.toElement;
46         } else if (event.fromElement) {
47             return event.fromElement;
48         } else { return null; }
49 
50     }
51 
52 }

 

posted @ 2015-04-10 14:05  helloZou  阅读(137)  评论(0编辑  收藏  举报