闭包问题...<body>
<div id='a'>
<div style=" width:120px; height:20px; background-color:#0000FF"></div>
<div style=" width:120px; height:20px; background-color:#FF0000"></div>
<div style=" width:120px; height:20px; background-color:#33FF00"></div>
<div style=" width:120px; height:20px; background-color:#FFFF00"></div>
</div>
<script>
var divs = document.getElementById("a").getElementsByTagName("div");
for (var i = 0; i < divs.length; i++)
{
(function(i){
divs[i].onclick =function(){alert(i+1);return false;};
})(i);
}
</script>
</body>
<div id='a'>
<div style=" width:120px; height:20px; background-color:#0000FF"></div>
<div style=" width:120px; height:20px; background-color:#FF0000"></div>
<div style=" width:120px; height:20px; background-color:#33FF00"></div>
<div style=" width:120px; height:20px; background-color:#FFFF00"></div>
</div>
<script>
var divs = document.getElementById("a").getElementsByTagName("div");
for (var i = 0; i < divs.length; i++)
{
(function(i){
divs[i].onclick =function(){alert(i+1);return false;};
})(i);
}
</script>
</body>
<body>
<div id='a'>
<div style=" width:120px; height:20px; background-color:#0000FF"></div>
<div style=" width:120px; height:20px; background-color:#FF0000"></div>
<div style=" width:120px; height:20px; background-color:#33FF00"></div>
<div style=" width:120px; height:20px; background-color:#FFFF00"></div>
</div>
<script>
var divs = document.getElementById("a").getElementsByTagName("div");
for (var i = 0; i < divs.length; i++)
{
divs[i].onclick = eval("new Function('alert('+i+');return false');");
}
</script>
</body>这样可以满足lz的需求。