Tag: Name exists multiple Eve classname red recursive ack obj
Event binding and drill-down with multiple window.onload execution
How to make the execution of two window.onload
Window.onload=function () {
Alert (' Lee ');
}
if (typeof window.onload== ' function ') {
var saved=null; Save the previous event object
Saved=window.onload;
}
Saved is window.onload,saved () equivalent to Window.onload (), but window.onload () cannot perform the
So saved () equals Window.onload=function () {}
Window.onload=function () {
if (saved) saved (); Perform the previous event
alert (Mr.Lee); Perform this event
}
----------------------------------------------------
Event switcher
<style>
. red{
width:100px;
height:100px;
background:red;
}
. blue{
width:100px;
height:100px;
Background:blue;
}
</style>
<div id= "box" class= "Red" > Test div</div>
Adding event functions
obj equals window
Type is equivalent to onload
fn equals function () {}
function Addevent (OBJ,TYPE,FN) {
Used to save the previous event
var saved=null;
Determine if an event exists
if (typeof obj[' on ' +type]== ' function ') {
saved=obj[' on ' +type]; Save Previous event
}
Execution events
obj[' on ' +type]=function () {
if (saved) saved ();
Fn.call (this); Pass this to the past
}
}
addevent (window, ' Load ', function () {
Alert (' Lee ');
})
addevent (window, ' Load ', function () {
Alert (' Mr.Lee ');
})
addevent (window, ' Load ', function () {
Alert (' Miss.lee ');
})
The following code event switches:
addevent (window, ' Load ', function () {
var Box=document.getelementbyid (' box ');
addevent (box, ' click ', function () {
Alert (' Lee ');
});
addevent (box, ' click ', Toblue);
})
function tored () {
This.classname= ' Red ';
Removeevent (This, ' click ');
Addevent (This, ' click ', Toblue);
}
function Toblue () {
This.classname= ' Blue ';
Removeevent (This, ' click ');
Addevent (This, ' click ', tored);
}
When constantly switching, the browser suddenly stuck, and error: Too much recursion, too many recursion
Because of the accumulation of too many saved events,
The solution is to use up the event and remove it immediately.
function Removeevent (obj,type) {
if (obj[' on ' +type]) obj[' on ' +type]=null;
}
Event bindings and in-depth-multiple window.onload