There are two types of event bindings, traditional event binding, and modern event binding. Traditional event bindings have inline mode and script mode. The script pattern assigns a function body to an object's event properties.
window.onload=function() { var domb=document.getelementbyid (' BD '); Domb.onclick=function() { alert (' button click ');};
};
Problems with traditional event bindings:
1. Event properties of the same object multiple functions are assigned many times, and the front is overwritten, showing only the last one.
Workaround, save the event for the first function and then execute it again.
window.onload=function() { alert (' Q '); }; if (typeof window.onload== ' function ') { var saved=null; Saved=window.onload; } Window.onload=function() { saved (); Alert (' s '); };
2. Switching of different events under the same event type
Workaround: When the first event is executed, the second event is assigned a click event in the first event function.
Wrap the Add event in a function. and solve the above problems together.
A key-value pair inside an object can also be accessed as an array, with attributes placed in brackets. An event function, which should not put the same name//event function.
function addevent (OBJ,TYPE,FN) { var saveevent=null; if (typeof obj[' on ' +type]== ' function ') saveevent=obj[' on ' +type]; obj[' on ' +type]=function() { if(saveevent) saveevent (); Fn.call (this);} ;}
Must be removed after adding an event, or memory will overflow
// Delete Event function removeevent (obj,type) { if(obj[' on ' +type]) obj[' on ' +type]=null; }
Problems that exist:
1. How to avoid adding functions that have already been added. You need to traverse the event, with duplicate names not added.
2. Delete an event, the exact deletion of an object, a type, a function of a name. Avoid accidental deletion.
Event Bindings 1