2010-04-14 49 views
0

我在函數init()中有以下循環;這是執行「onload」,我有traouble附加每個標記的mouseover事件,警報我總是返回最終循環/迭代(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]); 


    } 

回答

1

多小時後,我設法找到解決辦法:

window['popup'+i].events.register('mouseover', window['z'+i], function(e){ // pass through popup object 
       this.show(); // show popup 

});