Pure JavaScript-compliant Add and remove event encapsulation _javascript tips for browsers

Source: Internet
Author: User
Tags netbeans
Event processing compatible with a variety of browsers, using the ability to detect methods, the so-called ability to detect, that is, ability to do, do not have the ability to do//define an object to handle events, compatible with various browsers, DOM2 level event handling and IE events, if these two events are incompatible, use the DOM0 level Eventutil ={addevent:function (element,type,handler) {if (Element.addeventlistener) {//non IE browser with DOM2-level event handling , type is an event type such as: Click,handler is an event handler, and false represents an event with a bubbling model, if true represents a capture-processing model, or a capture-processing model in addition to NetBeans, and the other uses a bubble-handling model/
      If the non-IE browser add event is: AddEventListener Element.addeventlistener (Type,handler,false);
      }else if (element.attachevent) {//If for IE browser, add event using Attachevent element.attachevent (' on ' +type,handler);
      }else{element[' on ' +type] = handler; Removeevent:function (Element,type,handler) {if (Element.removeeventlistener) {//Non IE browser takes dom2 level event
      	Processing, type for event types such as: Click,handler is an event handler, false represents an event with a bubbling model, and if true represents a capture-processing model and a capture-processing model in addition to NetBeans, others adopt a bubble-handling model
      If the non-IE browser add event is: RemoveEventListener Element.removeeventlistener (Type,handler,false);
}else if (element.detachevent) {      	If for IE browser, add events using DetachEvent element.detachevent (' on ' +type,handler);
      }else{//dom0 Level Event handling, if the deletion event takes the assignment null element[' on ' +type] = null;
    Getevent:function (Event) {//Get event itself return event?event:window.event;
    Gettype:function (Event) {//Get event type return event.type;
    }, Getelement:function (event) {//Get Events Action element return Event.target | | event.srcelement; 
      {Preventdefault:function (event) {//block default event behavior if (event.preventdefault) {event.preventdefault ();
      }else{Event.returnvalue = false; }}, Stoppropagation:function (event) {//Stop event bubbling if (event.stoppropagation) {Event.stoppropagatio
    	  N ();
    	  }else{event.cancelbubble = true; }
    }




 }
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.