下面代码每次点击小框应该显示1-20编号
但是老显示21这一个数字
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style media="screen">
#containner
{
background-color:#FC0;
width:200px;
height:200px;
position:relative;
}
</style>
<script language="javascript">
function liyong()
{
document.getElementById("containner").innerHTML="";
var i=1
for (i=1;i<=20;i++)
{
var litdiv = document.createElement("div");
litdiv.style.backgroundColor="#0C9";
litdiv.style.borderColor="#FC0";
litdiv.style.width="20px";
litdiv.style.height="20px";
litdiv.style.position="absolute"; litdiv.style.top=Math.random()*180+"px";
litdiv.style.left=Math.random()*180+"px";
litdiv.onclick=function(){
window.alert("这个小框的编号是:"+i);
}
document.getElementById("containner").appendChild(litdiv);
}
}
</script>
</head><body>
<div id="containner">200X200的容器框!</div>
<input name="" type="button" value="点击按钮生成小框" onclick="javascript:liyong()"/>
</body>
</html>
但是老显示21这一个数字
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style media="screen">
#containner
{
background-color:#FC0;
width:200px;
height:200px;
position:relative;
}
</style>
<script language="javascript">
function liyong()
{
document.getElementById("containner").innerHTML="";
var i=1
for (i=1;i<=20;i++)
{
var litdiv = document.createElement("div");
litdiv.style.backgroundColor="#0C9";
litdiv.style.borderColor="#FC0";
litdiv.style.width="20px";
litdiv.style.height="20px";
litdiv.style.position="absolute"; litdiv.style.top=Math.random()*180+"px";
litdiv.style.left=Math.random()*180+"px";
litdiv.onclick=function(){
window.alert("这个小框的编号是:"+i);
}
document.getElementById("containner").appendChild(litdiv);
}
}
</script>
</head><body>
<div id="containner">200X200的容器框!</div>
<input name="" type="button" value="点击按钮生成小框" onclick="javascript:liyong()"/>
</body>
</html>
for 里面
litdiv.onclick=(function(num){
return function (){
window.alert("这个小框的编号是:"+num);
}
})(i);
document.getElementById("containner").innerHTML="";
var i=1
for (i=1;i<=20;i++){
var litdiv = document.createElement("div");
litdiv.style.backgroundColor="#0C9";
litdiv.style.borderColor="#FC0";
litdiv.style.width="20px";
litdiv.style.height="20px";
litdiv.style.position="absolute"; litdiv.id=i;
litdiv.style.top=Math.random()*180+"px";
litdiv.style.left=Math.random()*180+"px"; document.getElementById("containner").appendChild(litdiv); document.getElementById(i).onclick=function(){
window.alert("这个小框的编号是:"+this.id);
}
}
}
或者
litdiv.setAttribute('num',i);
litdiv.onclick=function (){
alert(this.getAttribute('num'));
};
{var litdiv = document.createElement("div");
litdiv.style.backgroundColor="#0C9";
litdiv.style.borderColor="#FC0";
litdiv.style.width="20px";
litdiv.style.height="20px";
litdiv.style.position="absolute";litdiv.style.top=Math.random()*180+"px";
litdiv.style.left=Math.random()*180+"px";
var a = i;//这么改写
litdiv.onclick=function(){
window.alert("这个小框的编号是:"+a);
}就是作用域的问题.改写下就好了.
当点击小div的时候,调用alert(i),此时的i必然是21,不可能会再循环。
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style media="screen">
#containner
{
background-color:#FC0;
width:200px;
height:200px;
position:relative;
}
</style>
<script language="javascript">
function liyong()
{document.getElementById("containner").innerHTML="";
var i=1
for (i=1;i<=20;i++)
{var litdiv = document.createElement("div");
litdiv.style.backgroundColor="#0C9";
litdiv.style.borderColor="#FC0";
litdiv.style.width="20px";
litdiv.style.height="20px";
litdiv.style.position="absolute";litdiv.style.top=Math.random()*180+"px";
litdiv.style.left=Math.random()*180+"px";
litdiv.onclick=function(i){
return function(){
window.alert("这个小框的编号是:"+i);
}
}(i)
document.getElementById("containner").appendChild(litdiv);}
}
</script>
</head><body>
<div id="containner">200X200的容器框!</div>
<input name="" type="button" value="点击按钮生成小框" onclick="javascript:liyong()"/>
</body>
</html>
看来闭包的问题对大家都是敏感的啊
LZ 楼上“大嘴哥”的那个答案OK