日期:2014-05-16 浏览次数:20485 次
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<input type="text" name="wb" />
<input type="text" name="wb"/>
<input type="text" name="wb"/>
<input type="text" name="wb"/>
<input type="text" name="wb"/>
<input type="text" name="wb"/>
<script>
function change(obj){
alert(obj);//这里的i是6
}
(function(){
var array = document.getElementsByName('wb');
for(var i=0;i<array.length;i++){
//alert(i);这里面i的值是012345;
array[i].attachEvent("onmouseover",function(){
alert(i); //这里的i是6
change(array[i]);});
}
change(i);
})();
</script>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<input type="text" name="wb" />
<input type="text" name="wb"/>
<input type="text" name="wb"/>
<input type="text" name="wb"/>
<input type="text" name="wb"/>
<input type="text" name="wb"/>
<script>
function change(obj){
alert(obj);//这里的i是6
}
(function(){
var array = document.getElementsByName('wb');
for(var i=0;i<array.length;i++){
//alert(i);这里面i的值是012345;
/*
array[i].attachEvent("onmouseover",function(){
alert(i); //这里的i是6
change(array[i]);});
*/
(function(key){
array[key].onmouseover = function(){alert(key)};
})(i)
}
})();
</script>
</body>
</html>
------解决方案--------------------
饿 第二个问题1楼理解得正确
for循环是没有自己的作用域
for(var i=0;i<=3;i++)
{
var aaa=i;