首页 > 代码库 > Js事件对象EventUtil

Js事件对象EventUtil

 1 var EventUtil = { 2     getEvent: function(event){       //获取事件对象 3         return event ? event : window.event; 4     }, 5     addHandler: function(element, type, handler){ //绑定事件 6         if (element.addEventListener) { 7                 element.addEventListener(type, handler, false); 8         } else if (element.attachEvent) { 9                 element.attachEvent("on" + type, handler);10         } else {11                 element["on" + type] = handler;12             13         }14     },15     removeHandler: function(element, type, handler){   //移除绑定16         if(element.removeEventListener){17             element.removeEventListener(type,handler,false);18         }else if(element.detachEvent){19             element.detachEvent("on"+type,handler,false);20         }else{21             element["on"+type] = null;22         }23     },24     preventDefault: function(event){   //阻止默认事件25         if(event.preventDefault){26             event.preventDefault();27         }else{28             event.returnValue = http://www.mamicode.com/false;29         }30     },31     stopPropagation: function(event){   //阻止事件冒泡32         if (event.stopPropagation) {33             event.stopPropagation();34         }else{35             event.cancelBubble = true;36         }37     },38     getRelateTarget: function(event){ //事件相关元素的信息39         if(event.relatedTarget){40             return event.relatedTarget;41         }else if(event.toElement){42             event.toElement;43         }else if(event.fromElement){44             return event.fromElement;45         }else{46             return null;47         }48     },49     getButton: function(event){  //检测button属性50         if(document.implementation.hasFeature("MouseEvents","2.0")){51             return event.button;52         }else{53             switch(event.button){54                 case 0:55                 case 1:56                 case 3:57                 case 5:58                 case 7:59                         return 0;//主鼠标按钮60                 case 2:61                 case 6:62                         return 2;//次鼠标按钮63                 case 4:64                         return 1;//鼠标滚轮按钮65             }66         }67     },68     getWheelDelta: function(event){//鼠标滚动属性值69         if(event.wheelDelta){70             return (client.engine.opera && client.engine.opera <9.5  ? -event.wheelDelta:event.wheelDelta)71         }else{72             return -event.detail *40;73         }74     },75     getCharCode: function(event){//获取字符编码76         if(typeof event.charCode == "number"){77             return event.charCode;78         }else{79             return event.keyCode;80         }81     },82     getCilpboardText: function(event){//获取剪切板内容83         var cilpboardData = http://www.mamicode.com/(event.clipboardData || window.clipboardData);84         return clipboardData.getData("text");85     },86     setClipboardText:function(event,value){//剪切到剪切板87         if(event.clipboardData){88             return event.clipboardData.setData("text/plain",value);89         }else if(window.clipboardData){90             return window.clipboardData.setData("text",value);91         }92     }93 94 95 }

代码总结于《JavaScript高级程序设计》事件章节。