您的位置:首页 > Web前端 > JavaScript

js闭包演示

2009-06-17 11:11 302 查看
有个网友问了个问题,如下的html,为什么每次输出都是5

Html代码

<html >

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>闭包演示</title>

<style type="text/css">

</style>

<script type="text/javascript">

function init() {

var pAry = document.getElementsByTagName("p");

for( var i=0; i<pAry.length; i++ ) {

pAry[i].onclick = function() {

alert(i);

}

}

}

</script>

</head>

<body onload="init();">

<p>产品一</p>

<p>产品一</p>

<p>产品一</p>

<p>产品一</p>

<p>产品一</p>

</body>

</html>

解决方式有两种,

1、将变量 i 保存给在每个段落对象(p)上

Js代码

function init() {

var pAry = document.getElementsByTagName("p");

for( var i=0; i<pAry.length; i++ ) {

pAry[i].i = i;

pAry[i].onclick = function() {

alert(this.i);

}

}

}

2、将变量 i 保存在匿名函数自身

Java代码

function init2() {

var pAry = document.getElementsByTagName("p");

for( var i=0; i<pAry.length; i++ ) {

(pAry[i].onclick = function() {

alert(arguments.callee.i);

}).i = i;

}

}

再增加3种

3、加一层闭包,i以函数参数形式传递给内层函数

Js代码

function init3() {

var pAry = document.getElementsByTagName("p");

for( var i=0; i<pAry.length; i++ ) {

(function(arg){

pAry[i].onclick = function() {

alert(arg);

};

})(i);//调用时参数

}

}

4、加一层闭包,i以局部变量形式传递给内存函数

Js代码

function init4() {

var pAry = document.getElementsByTagName("p");

for( var i=0; i<pAry.length; i++ ) {

(function () {

var temp = i;//调用时局部变量

pAry[i].onclick = function() {

alert(temp);

}

})();

}

}

5、加一层闭包,返回一个函数作为响应事件(注意与3的细微区别)

Js代码

function init5() {

var pAry = document.getElementsByTagName("p");

for( var i=0; i<pAry.length; i++ ) {

pAry[i].onclick = function(arg) {

return function() {//返回一个函数

alert(arg);

}

}(i);

}

}

又有一种方法

6、用Function实现,实际上每产生一个函数实例就会产生一个闭包

Js代码

function init6() {

var pAry = document.getElementsByTagName("p");

for( var i=0; i<pAry.length; i++ ) {

pAry[i].onclick = new Function("alert(" + i + ");");//new一次就产生一个函数实例

}

}

from:http://zhouyrt.javaeye.com/blog/250073
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: