错误的写法
var i, afor (i = 0; i < 10; i++) { a = document.createElement('a') a.innerHTML = i + '' a.addEventListener('click', function (e) { e.preventDefault() alert(i) }) document.body.appendChild(a)}
正确的写法
var ifor (i = 0; i < 10; i++) { (function (i) { var a = document.createElement('a') a.innerHTML = i + '' a.addEventListener('click', function (e) { e.preventDefault() alert(i) }) document.body.appendChild(a) })(i)}