JavaScript Browser compatible Eventutil

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.