<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>闭包存循环的索引值</title>
<style type="text/css">
li{
height:30px;
background-color: #2cbeff;
margin-bottom: 10px;
}
</style>
<script type="text/javascript">
//闭包的用途:存循环的索引值
window.onload = function(){
var aLi = document.getElementsByTagName('li');
// alert(aLi.length);
for(var i = 0; i<aLi.length; i++){
(function(i){
aLi[i].onclick = function(){
alert(i);
}
})(i);
}
}
</script>
</head>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
</ul>
</body>
</html>