jquery迴圈繫結click事件
阿新 • • 發佈:2019-01-29
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <script type="text/javascript" src="js/jquery-1.8.3.js"></script> <script> $(function() { for(var i = 0; i < 10; i++) { $("#" + i).bind("click", { index: i }, clickHandler); } function clickHandler(event) { var i = event.data.index; alert(i); } }); </script> <body> <input type="button" value="button0" id='0' /> <input type="button" value="button1" id='1' /> <input type="button" value="button2" id='2' /> <input type="button" value="button3" id='3' /> <input type="button" value="button4" id='4' /> <input type="button" value="button5" id='5' /> <input type="button" value="button6" id='6' /> <input type="button" value="button7" id='7' /> <input type="button" value="button8" id='8' /> <input type="button" value="button9" id='9' /> </body> </html>