The code copy is as follows:
</pre><pre name="code"><html>
<body>
<input type="button" name="input[]" value="button1" /><br />
<input type="button" name="input[]" value="button2" /><br />
<input type="button" name="input[]" value="button 3" /><br />
<div id="add"></div>
</body>
</html>
<script type="text/javascript">
// Get all input controls through getElementsByTagName
var inputs =document.getElementsByTagName("input");
// Bind the onclick event for the 0th button, alert
inputs[0].onclick = function(){
alert("I'll test it");
}
// Bind the onclick event for each button, alert
for(var i=0;i<inputs.length;i++){
inputs[i].onclick = function(){
alert("I'll test it");
}
}
window.onload = function(){
// Define an array arrrs
var arrrs = new Array();
// Loop addition
for(var i=0;i<2;i++){
// Add two input type="button" value="new" +i
var input = document.createElement("input");
input.type = "button";
input.value = "Add" + i;
// Remember to put the created input into arrrs
arrrs.push(input);
// Then put input into the div with id="add"
document.getElementById("add").appendChild(input);
}
// Also use [0].onclick to bind the event, there is still no problem
arrrs[0].onclick=function(){
alert("I'll test it again");
}
}
</script>