<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js闭包</title>
</head>
<body>
<ul>
<li>Document1</li>
<li>Document2</li>
<li>Document3</li>
<li>Document4</li>
<li>Document5</li>
</ul>
</body>
<script>
var ul =document.getElementsByTagName("ul");
var li =ul[0].getElementsByTagName('li');
for (var i = 0; i < li.length; i++) {
li[i].index=i;
li[i].οnclick=function () {
console.log(this.index)
}
}
</script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js闭包</title>
</head>
<body>
<ul>
<li>Document1</li>
<li>Document2</li>
<li>Document3</li>
<li>Document4</li>
<li>Document5</li>
</ul>
</body>
<script>
var ul =document.getElementsByTagName("ul");
var li =ul[0].getElementsByTagName('li');
for (var i = 0; i < li.length; i++) {
(function(j){
li[j].οnclick=function(){
console.log(j)
}
}(i))
}
</script>
</html>