Copy codeThe Code is as follows:
</Pre> <pre name = "code" class = "javascript"> <Body>
<Input type = "button" name = "input []" value = "button 1"/> <br/>
<Input type = "button" name = "input []" value = "button 2"/> <br/>
<Input type = "button" name = "input []" value = "button 3"/> <br/>
<Div id = "add"> </div>
</Body>
</Html>
<Script type = "text/javascript">
// The input control is obtained through getElementsByTagName.
Var inputs = document. getElementsByTagName ("input ");
// Bind The onclick event to 0th buttons. Click alert.
Inputs [0]. onclick = function (){
Alert ("let me test ");
}
// Bind an onclick event to each button. Click alert.
For (var I = 0; I <inputs. length; I ++ ){
Inputs [I]. onclick = function (){
Alert ("let me test ");
}
}
Window. onload = function (){
// Define an array arrs
Var arrs = new Array ();
// Cyclically add
For (var I = 0; I <2; I ++ ){
// Cyclically add two input types = "button" value = "add" + I
Var input = document. createElement ("input ");
Input. type = "button ";
Input. value = "add" + I;
// Remember to put the created input into arrs
Arrs. push (input );
// Put the input in the div id = "add"
Document. getElementById ("add"). appendChild (input );
}
// Bind the event with the same [0]. onclick. the problem persists.
Arrs [0]. onclick = function (){
Alert ("test again ");
}
}
</Script>