标签:
常见闭包错误
==html==
```
html
```
<div id="div1">aaa</div>
<div id="div2">bbb</div>
<div id="div3">ccc</div>
```
++想要点击aaa出现1,bbb2,ccc3...++
==常见错误js==
```
for (var i = 1; i < 4; i++) {
document.getElementById(‘div‘ + i).addEventListener(‘click‘, function () {
console.log(i)
});
}
```
==改正==
```
for (var i = 1; i < 4; i++) {
!function (i) {
document.getElementById(‘div‘ + i).addEventListener(‘click‘, function () {
console.log(i)
});
}(i)
}
```
标签:
原文地址:http://www.cnblogs.com/luckstart/p/5204629.html