<!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>为啥i最后的值是6啊,,为啥i在for循环外面还可以用啊?求高手,,,,,,,,
解决方案 »
- IE6下PNG透明处理之后引发的一些问题,你们有研究过吗?
- js省市二级联动问题?????????
- js下接菜单在ie6中不能使用
- 帮忙看看这个JavaScript分页程序,为何翻到下一页就不能翻回了?
- 如何才可以再.CS文件中写东西
- 我想找出文本框里哪些被选中了,哪些在选中的前面,哪些在选中块的后面,怎么弄?
- 请教,日期比较.
- 如何在网页中读入本地硬盘的一个TXT,在写入areatext中!!!
- javascript框架及控件开发
- 关于DHTML中给SELECT动态增加一个OPTION的问题
- 求助在线等,请大侠们帮帮忙,在ff上运行正常,ie上不行
- 数组中去除重复元素 求解释这段代码或更好方法
因为for循环的执行过程,在最后一次循环(i=5)后,还要执行一次i++,然后判断条件i<array.length,发现不满足,于是退出循环,所以循环结束后i=6
至于在for循环外部使用,这是js的问题了,i实际上相当于在外部定义的
var i;
for(i=0;i<array.length;i++)所以
for(var i=0,abc=123;i<array.length;i++)
{
}
alert(abc);
像这种,abc也是可以在后面访问到的.....
蓝色部分是在一次循环结合后执行的,i等于5时,进入循环,然后结束循环时执行i++,i=6,此时就不符合进入循环的条件了别人家这么写你也这么写,先理解了意思。<!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>
{
var aaa=i;
}
alert(aaa);
这样也都可以访问
别人那么写我只能这么写啊,我下的JS电子书都没说过JS没有块作用域啊,,
function(){
alert(i); //这里的i是6
change(array[i]);});
}这时循环肯定执行完毕了,所以i 是 6.
var array = document.getElementsByName('wb');
for(var i=0;i<array.length;i++){
array[i].index=i;
array[i].attachEvent("onmouseover",function(){
alert(this.index);
});
}
<!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(j){
return function(){
alert(j);
change(array[j]);
}
})(i));
}
//change(i);
})();</script>
</body>
</html>
<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(j){
return function(){
alert(j);
change(array[j]);
}
})(i));
}
//change(i);
})();
</script>
</body>
</html>