日期:2014-05-16  浏览次数:20339 次

求一个脚本写法


markers[0].addEventListener("mouseover", function(){this.openInfoWindow(infoWindows[0]);});
markers[1].addEventListener("mouseover", function(){this.openInfoWindow(infoWindows[1]);});
markers[2].addEventListener("mouseover", function(){this.openInfoWindow(infoWindows[2]);});
markers[3].addEventListener("mouseover", function(){this.openInfoWindow(infoWindows[3]);});
markers[4].addEventListener("mouseover", function(){this.openInfoWindow(infoWindows[4]);});
markers[5].addEventListener("mouseover", function(){this.openInfoWindow(infoWindows[5]);});
markers[6].addEventListener("mouseover", function(){this.openInfoWindow(infoWindows[6]);});
markers[7].addEventListener("mouseover", function(){this.openInfoWindow(infoWindows[7]);});
markers[8].addEventListener("mouseover", function(){this.openInfoWindow(infoWindows[8]);});
markers[9].addEventListener("mouseover", function(){this.openInfoWindow(infoWindows[9]);});



上面的规律写法,我想用一个循环解解决,结果是错误的,求正确写法,谢谢
错误的写法如下
for(i=0;i<markers.length;i++){

markers[i].addEventListener("mouseover", function(){this.openInfoWindow(infoWindows[i]);});
}

------解决方案--------------------
for(i=0;i<markers.length;i++){
 markers[i].addEventListener("mouseover", (function(i,obj){
  return function(){obj.openInfoWindow(infoWindows[i]); }
 })(i,this) );
}