JS event handler Cross browser solution _javascript tips

Source: Internet
Author: User

This article examples for you to share the JS event handler Cross-browser solution for your reference, the specific content as follows

<!
DOCTYPE html>  

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.

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.