JavaScript在循环绑定时候会出现变量污染的情况。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>变量污染</title> </head> <body> <div class="box">000000001</div> <div class="box">000000002</div> <div class="box">000000003</div> <script> var divs = document.querySelectorAll('.box') for (var i=0;i<divs.length;i++){ divs[i].onclick = function () { console.log(i) } } </script> </body> </html>