动态创建div的问题

来源:百度知道 编辑:UC知道 时间:2024/06/30 05:07:32
<script language="javascript">
<!--
function test(){
var obj=document.getElementById("test");
for(var i=0;i<6; i++){
var testDiv=document.createElement("div");
testDiv.setAttribute("id","son"+i);
testDiv.style.cursor="pointer";
testDiv.title=i;
testDiv.onclick=function(){alert(i);}
testDiv.innerHTML="No."+i;
obj.appendChild(testDiv);
}
}
//-->
</script>
<body onload="test()">
<div id="test"></div>
</body>
wolf~伟(312322335) 13:45:11
这个onclick如果调用一个函数,传递i这个值为什么总是它的最大值呢

<script language="javascript">
<!--
function tt(nod)
{
this.clickFunc = function()
{
alert(nod);
}
}

function test(){
var obj=document.getElementById("test");
for(var i=0;i<6; i++){
var testDiv=document.createElement("div");
testDiv.setAttribute("id","son"+i);
testDiv.style.cursor="pointer";
testDiv.title=i;

var col = new tt(i);
testDiv.onclick = col.clickFunc;

testDiv.innerHTML="No."+i;
obj.appendChild(testDiv);
}
}
//-->
</script>
<body onLoad="test()">
<div id="test"></div>
</body>