event related object in javascript

  1 var EventUtil = {
  2 
  3         addHandler: function (element, type, handler) {
  4             if (element.addEventListener) {
  5                 element.addEventListener(type, handler, false);
  6             } else if (element.attachEvent) {
  7                 element.attachEvent("on" + type, handler);
  8             } else {
  9                 element["on" + type] = handler;
 10             }
 11         },
 12 
 13         removeHandler: function (element, type, handler) {
 14             if (element.removeEventListener) {
 15                 element.removeEventListener(type, handler, false);
 16             } else if (element.detachEvent) {
 17                 element.detachEvent("on" + type, handler);
 18             } else {
 19                 element["on" + type] = null;
 20             }
 21         },
 22 
 23         getEvent: function (event) {
 24             return event ? event : window.event;
 25         },
 26 
 27         getTarget: function (event) {
 28             return event.target || event.srcElement;
 29         },
 30 
 31         preventDefault: function (event) {
 32             if (event.preventDefault) {
 33                 event.preventDefault();
 34             } else {
 35                 event.returnValue = false;
 36             }
 37         },
 38 
 39         stopPropagation: function (event) {
 40             if (event.stopPropagation) {
 41                 event.stopPropagation();
 42             } else {
 43                 event.cancelBubble = true;
 44             }
 45         },
 46 
 47         getRelatedTarget: function (event) {
 48             if (event.relatedTarget) {
 49                 return event.relatedTarget;
 50             } else if (event.toElement) {
 51                 return event.toElement;
 52             } else if (event.fromElement) {
 53                 return event.fromElement;
 54             } else {
 55                 return null;
 56             }
 57         },
 58 
 59         getButton: function (event) {
 60             if (document.implementation.hasFeature("MouseEvents", "2.0")) {
 61                 return event.button;
 62             } else {
 63                 switch (event.button) {
 64                     case 0:
 65                     case 1:
 66                     case 3:
 67                     case 5:
 68                     case 7:
 69                         return 0;
 70                     case 2:
 71                     case 6:
 72                         return 2;
 73                     case 4:
 74                         return 1;
 75                 }
 76             }
 77         },
 78 
 79         getCharCode: function (event) {
 80             if (typeof event.charCode == "number") {
 81                 return event.charCode;
 82             } else {
 83                 return event.keyCode;
 84             }
 85         },
 86 
 87         getWheelDelta: function (event) {
 88             if (event.wheelDelta) {
 89                 return (client.engine.opera && client.engine.opera < 9.5 ? -event.wheelDelta : event.wheelDelta);
 90             } else {
 91                 return -event.detail * 40;
 92             }
 93         },
 94 
 95         getClipboardText: function (event) {
 96             var clipboardDate = (event.clipboardDate || window.clipboardDate);
 97             return clipboardDate.getDate("text");
 98         },
 99 
100         setClipboardText: function (event, value) {
101             if (event.clipboardDate) {
102                 return event.clipboardDate.setDate("text/plain", value);
103             } else if (window.clipboardDate) {
104                 return window.clipboardDate.setDate("text", value);
105             }
106         }
107     };

 

posted on 2013-05-16 17:28  雷音  阅读(252)  评论(0编辑  收藏  举报

导航