<div class="codetitle"> <span><a style="CURSOR: pointer" data="11599" class="copybut" id="copybut11599" onclick="doCopy('code11599')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code11599"> <br><input type="button" value="btn1" id="btn1"> <br><input type="button" value="btn2" id="btn2"> <br><input type="button" value="btn3" id="btn3"> <br><script type="text/javascript"> <BR>for(var i=1;i<=3;i++){ <BR>document.getElementById('btn' + i).onclick = (function(j){ <BR>return function(){ <BR>alert(j); <BR>} <BR>})(i); <BR>} <BR></script><br> </div> <br><strong>下面是结合了jquery的代码,原理都是一样的<br></strong>在使用javascript批量绑定页面上的元素并传递递增编号时,需要如下编写: <br>部分的代码: <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="3313" class="copybut" id="copybut3313" onclick="doCopy('code3313')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code3313"> <br><input type="button" id="btn1" value="按钮1"> <br><input type="button" id="btn2" value="按钮2"> <br><input type="button" id="btn3" value="按钮3"> <br><input type="button" id="btn4" value="按钮4"> <br><input type="button" id="btn5" value="按钮5"> <br><input type="button" id="btn6" value="按钮6"> <br><input type="button" id="btn7" value="按钮7"> <br> </div> <br><js>部分的代码: <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="81287" class="copybut" id="copybut81287" onclick="doCopy('code81287')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code81287"> <br>$(function () { <br>var i; <br>for (i = 1; i $("#btn" + i).bind("click", { id: i }, dis); <br>} <br>}); <br><br>function dis(evt) { <br>alert(evt.data.id); <br>} <br> </div> </js>