JavaScript cross-browser event processing tool

Source: Internet
Author: User

/** * Cross-browser event processing tool. Only bubbles are supported. Capture not supported * @author ([email protected]) */var eventutil = {Getevent:function (event) {return Event | | windo    W.event;    }, Gettarget:function (event) {return Event.target | | event.srcelement;            },//Return registered successful listener, ie need to use return value to remove listener on:function (Elem, type, handler) {if (Elem.addeventlistener) {            Elem.addeventlistener (type, handler, false);        return handler;              } else if (elem.attachevent) {var wrapper = function () {var event = window.event;              Event.target = event.srcelement;            Handler.call (Elem, event);            };            Elem.attachevent (' on ' + type, wrapper);        return wrapper; }}, Off:function (Elem, type, handler) {if (Elem.removeeventlistener) {Elem.removeeventlistene        R (Type, Handler, false);        } else if (elem.detachevent) {elem.detachevent (' on ' + type, handler); }}, PreveNtdefault:function (Event) {if (Event.preventdefault) {event.preventdefault ();        } else if (' returnvalue ' in event) {Event.returnvalue = false;        }}, Stoppropagation:function (event) {if (event.stoppropagation) {event.stoppropagation ();        } else if (' cancelbubble ' in event) {event.cancelbubble = true;        }},/** * keypress events get input characters across browsers * Some browsers also trigger keypress on some special keys, which returns null **/getchar:function (event) {  if (Event.which = = null) {return string.fromcharcode (Event.keycode); IE} else if (Event.which! = 0 && Event.charcode! = 0) {return String.fromCharCode (EV    Ent.which);    The rest} else {return null; Special Key}};

Reference:
What is the DOM event model, writing a Eventutil tool class to implement event management compatibility

JavaScript cross-browser event processing tool

Related Article

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.