Copy codeThe Code is as follows:
<Input type = "button" value = "btn1" id = "btn1"/>
<Input type = "button" value = "btn2" id = "btn2"/>
<Input type = "button" value = "btn3" id = "btn3"/>
<Script type = "text/javascript">
For (var I = 1; I <= 3; I ++ ){
Document. getElementById ('btn '+ I). onclick = (function (j ){
Return function (){
Alert (j );
}
}) (I );
}
</Script>
The following is a combination of jquery code. The principles are the same.
When using javascript to batch bind elements on the page and pass the incremental number, you need to write the following code:
<Html> some code:
Copy codeThe Code is as follows:
<Input type = "button" id = "btn1" value = "button 1"/>
<Input type = "button" id = "btn2" value = "button 2"/>
<Input type = "button" id = "btn3" value = "button 3"/>
<Input type = "button" id = "btn4" value = "button 4"/>
<Input type = "button" id = "btn5" value = "button 5"/>
<Input type = "button" id = "btn6" value = "button 6"/>
<Input type = "button" id = "btn7" value = "button 7"/>
<Js> code:
Copy codeThe Code is as follows:
$ (Function (){
Var I;
For (I = 1; I <= 7; I ++ ){
$ ("# Btn" + I). bind ("click", {id: I}, dis );
}
});
Function dis (evt ){
Alert (evt. data. id );
}