Book: "Pro JavaScript techniques proficient in JavaScript" binding/removing event listening Functions

Source: Internet
Author: User
// Addevent/removeevent compiled by Dean Edwards. Code Some extensions/http://dean.edwards.name/weblog/2005/10/add-event/ made

 Function Addevent (element, type, Handler ){ // Assign an independent ID to each event processing function  If (! Handler. $ guid) handler. $ guid = addevent. guid ++; // Create an event type hash for the element  If (! Element. Events) element. Events = {}; // Create an event handler hash for each element/event  VaR Handlers = element. events [type]; If (! Handlers) {handlers = element. events [type] = {}; // Store existing event processing functions (if one already exists)  If (Element [ "On" + Type]) {handlers [ 0 ] = Element [ "On" + Type] ;}} // Store event processing functions in the scattered list Handlers [handler. $ guid] = handler; // Assign a global event processing function to handle all tasks Element [ "On" + Type] = handleevent ;}; // Create a counter with an independent ID Addevent. guid =1 ; Function Removeevent (element, type, Handler ){ // Delete the event handler from the hash.  If (Element. Events & element. events [type]) { Delete Element. events [type] [handler. $ guid] ;}}; Function Handleevent (event) {var returnvalue = true; // Obtain the event object (ie uses the global event object) Event = event | fixevent (window. Event ); // Obtain the reference of the event processing function hash list  VaR Handlers = This . Events [event. Type]; // Execute each event processing function in sequence  For ( VaR I In Handlers ){ This . $ Handleevent = handlers [I]; If (this. $ handlerevent (event) === false) {returnvalue = false ;} }; // Added the function fixevent (event) method that is missing some ie event objects {// added the W3C standard event Method event. preventdefault = fixevent. preventdefault; event. stoppropagation = fixevent. stoppropagation; return event;} fixevent. preventdefault = function () {This. returnvalue = false ;}; fixevent. stoppropagation = function () {This. cancelbubble = true ;};

================================================= ======================================< br> 1. addevent has the following advantages: Dean Edward's addevent event binding method can work in all browsers, even though it is older and does not support any browsers. This keyword can be used in all binding functions, it points to the current element and all browser-specific functions that prevent browser default behavior and prevent event bubbling. Regardless of the browser type, the event object is always passed in as the first object. disadvantages of addevent the following are the disadvantages of Dean Edward's addevent event binding method, which only works in the bubble stage (because it uses the traditional method of event binding in depth)

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.