varEventutil = {
Add event handler AddHandler:function(element, type, handler) {if(Element.addeventlistener) {Element.addeventlistener (type, handler,false);
The event handlers defined in DOM2, Ie9,firefox,safaei,chrome and opera support this method. } Else if(element.attachevent) {element.attachevent ("On" +Type, handler);
The attachevent () method in IE is run in the global scope, that is, this equals window. and if more than one handler is added to an event, the order of execution is reversed. } Else{element["On" + type] =handler;
The DOM0-level method, which only supports one event handler per event. } },
Remove event handler RemoveHandler:function(element, type, handler) {if(Element.removeeventlistener) {Element.removeeventlistener (type, handler,false);
When using the Remove method, its incoming parameters are the same as when the handler is added, which means that the added anonymous function will not be removed. } Else if(element.detachevent) {element.detachevent ("On" +type, handler);
As with the above, the anonymous function is not removed.
To solve this problem, we can pass in a reference to an anonymous function instead of passing it directly to the anonymous function. } Else{element["On" + type] =NULL;
DOM0-Level Method}},//Get Event object getEvent:function(event) {returnEvent?event:window.event;
In a DOM-compatible browser, the event is simply passed in and returned; in IE, the event is undefined (undefined), but a Window object property. },
Get event target Gettarget:function(event) {returnEvent.target | |event.srcelement;
The event target attribute in IE is srcelement. },
Cancel the default behavior of an event
The default behavior of an event can be canceled only if the Cancelable property is true. Preventdefault:function(event) {if(Event.preventdefault) {event.preventdefault (); } Else{Event.returnvalue=false;
The event object in IE has the ReturnValue property, the default is true, and set to False to cancel the default behavior. } },
To cancel the capture or bubbling stoppropagation of an event:function(event) {if(event.stoppropagation) {event.stoppropagation ();
Can be used to cancel event capture and bubbling. } Else{event.cancelbubbles=true;
Compatible with IE, you can only cancel event bubbling. }}, Getrelatedtarget:function(event) {if(Event.relatedtarger) {returnEvent.relatedtarget; } Else if(event.toelement) {returnevent.toelement; } Else if(event.fromelement) {returnevent.fromelement; } Else{return NULL; } }}
JavaScript Browser compatible Eventutil