This article examples for you to share the JS event handler Cross-browser solution for your reference, the specific content as follows
Again for everyone to share JS native event handling Cross-browser code:
Cross-browser Event processor Add var Eventutil = {addhandler:function (elem, type, handler) {if (Elem.addeventlistener) {ele
M.addeventlistener (type, handler, false);
} else if (elem.attachevent) {elem.attachevent ("on" + type, handler);//When adding multiple handler of the same type, the rule for IE is the first trigger that was last added
else{if (typeof elem["on" + type] = = ' function ') {var oldhandler = elem[' on ' + type];
Elem["on" + type] = function () {Oldhandler ();
Handler ();
} else{elem["on" + type] = handler;//support for adding multiple event handlers}}}, 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 = false; }, Removehandler:function (Elem, type, handler) {if (Elem.removeeventlistener) {Elem.removeevenTlistener (type, handler, false);
else if (elem.detachevent) {elem.detachevent ("on" + type, handler); else{elem["on" + type] = null;//does not support the removal of a single event handler and can only remove all}}, Stoppropagation:function (event) {if (E
vent.stoppropagation) {event.stoppropagation ();
} else{event.cancelbubble = true;
}, Getrelatedtarget:function (event) {if (event.relatedtarget) {return event.relatedtarget;
else if (event.toelement && event.type = = "Mouseout") {return event.toelement;
else if (event.fromelement && event.type = = "MouseOver") {return event.fromelement;
} else{return null; },/*ie8 the left and middle keys are 0;ff unrecognized middle key; Chrome normal */Getbutton:function (event) {//Return 0,1,2-left, middle, right if (Document.implementati
On.hasfeature ("Mouseevents", "2.0")) {return event.button; } else{switch (Event.button) {case 0:case 1:case 3:case 5:case 7:return 0;
Break
Case 2:case 6:return 2;
Break
Case 4:return 1;
Break
Default:break; }},/* can only detect KeyPress events, the return value is equal to the character encoding to be displayed//*ie and chrome only can display the word keys trigger, FF Other keys can also trigger, the return value is 0*/getcharcode:function (eve
NT) {if (typeof Event.charcode = = "Number") {return event.charcode;
} else{return event.keycode;
}
}
};
The above is the entire content of this article, I hope to solve the JS event handler for you to help across the browser.