闭包例子
闭输出小标 <script type="text/javascript"> //创建数组元素 var arr = new Array(); for(var i=0; i<4; i++){ arr[i] =snot(i) } function snot(n){ return function(){ console.log(n); } } arr[2](); //2 arr[0](); //0 arr[1](); //1 arr[3](); // </script> 闭包鼠标放上实现五颜六色的效果
<script type="text/javascript">
window.onload = function() {
var cars = document.getElementsByTagName('li');
var col = ['red', 'blue', 'green'];
for(var i=0; i<cars.length; i++){
cars[i].onmouseover=over(i);
}
function over(n){
return function(){
cars[n].style.background=col[n];
}
}
}
</script>