// 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)