我在函数init()中有以下循环;执行“onload”,我正在为每个标记附加鼠标悬停事件,警报我总是返回最终循环/迭代的值(228)?
for(i=0; i<document.getElementById('departureSelect').options.length; i++){
var coords = eval(document.getElementById('departureSelect').options[i].value);
if(i==0){
window['popup'+i] = new OpenLayers.Marker(new OpenLayers.LonLat(coords[0], coords[1]),icon);
}else{
window['popup'+i] = new OpenLayers.Marker(new OpenLayers.LonLat(coords[0], coords[1]),icon.clone());
}
window['z'+i] = new OpenLayers.Popup.Anchored(coords[2],
new OpenLayers.LonLat(coords[0], coords[1]),
new OpenLayers.Size(0,0),
'<span class="country-label">' + coords[2] + '</span>',
icon,
false
);
window['z'+i].autoSize = true;
window['z'+i].setBorder('1px solid #888');
map.addPopup(window['z'+i]);
window['z'+i].hide();
window['popup'+i].events.register('mouseover', window['popup'+i], function(e){
alert(i); // only returns final iteration of i (228)?????
});
countries.addMarker(window['popup'+i]);
}
答案 0 :(得分:1)
经过数小时的努力,我找到了解决方案:
window['popup'+i].events.register('mouseover', window['z'+i], function(e){ // pass through popup object
this.show(); // show popup
});