The differences between attachEvent and addEventListener adapt to different browser versions. during use, pay attention to the onclick button of the attachEvent method.
AddEventListener method button click
The usage principle of the two is as follows: the priority of the execution can be different. The example below is as follows:
The attachEvent method attaches other processing events to an event. (Mozilla series not supported)
The addEventListener method is used in the Mozilla series.
Example:
Java code
The Code is as follows:
Document. getElementById ("btn"). onclick = method1;
Document. getElementById ("btn"). onclick = method2;
Document. getElementById ("btn"). onclick = method3;
If this is the case, only medhot3 will be executed.
Write as follows:
Java code:
The Code is as follows:
Var btn1Obj = document. getElementById ("btn1 ");
// Object. attachEvent (event, function );
Btn1Obj. attachEvent ("onclick", method1 );
Btn1Obj. attachEvent ("onclick", method2 );
Btn1Obj. attachEvent ("onclick", method3 );
The execution sequence is method3-> method2-> method1
If the Mozilla series does not support this method, you need to use addEventListener.
Java code:
The Code is as follows:
Var btn1Obj = document. getElementById ("btn1 ");
// Element. addEventListener (type, listener, useCapture );
Btn1Obj. addEventListener ("click", method1, false );
Btn1Obj. addEventListener ("click", method2, false );
Btn1Obj. addEventListener ("click", method3, false );
The execution sequence is method1-> method2-> method3
Instance used:
1. Java code:
The Code is as follows:
Var el = EDITFORM_DOCUMENT.body;
// Get the object first. EDITFORM_DOCUMENT is actually an iframe
If (el. addEventListener )...{
El. addEventListener ('click', KindDisableMenu, false );
} Else if (el. attachEvent )...{
El. attachEvent ('onclick', KindDisableMenu );
}
2. Java code:
The Code is as follows:
If (window. addEventListener ){
Window. addEventListener ('load', _ uCO, false );
} Else if (window. attachEvent ){
Window. attachEvent ('onload', _ uCO );
}