<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>闭包变量</title> </head> <body> <ul> <li>第一个</li> <li>第二个</li> <li>第三个</li> <li>第四个</li> </ul> <script> var element = document.getElementsByTagName('li'); /*for(var i=0;i<element.length;i++){ element[i].index = i; element[i].onclick = function(){ console.log(this.index); } }*/ for(var i=0;i<element.length;i++){ element[i].onclick = function(index){ return function(){ console.log(index+'--->'); } }(i); } </script> </body> </html>