<ul> <li> index = 0 </li> <li> index = 1 </li> <li> index = 2 </li> <li> index = 3 </li> </ul> <script type="text/javascript"> var nodes = document.getElementsByTagName('li'); for(i = 0;i<nodes.length;i+=1) { nodes[i].onclick = function() { console.log(i+1); //不使用闭包的话,值每次都是4 }(4); } </script>