2011-05-24 136 views
0
function initialize(final) 
{ 
    if (GBrowserIsCompatible()) { 
      ................................................ 
     } 

     var address_array = final.split('~'); 

     for (var count = 0; count < address_array.length; count++) { 
      if (geocoder) { 
       geocoder.getLatLng(
     address_array[count], 
     makeTheFunction(address_array, count) 
    ); 
      } 
     } 

    } 



    function makeTheFunction(array, thisCount) { 
     return function (point) { 
      if (!point) { 
       alert(array[thisCount] + " not found"); 
      } 


      else { 
       var marker = new GMarker(point); 
       map.addOverlay(marker); 
       GEvent.addListener(marker, "click", function() { 
        marker.openInfoWindowHtml(array[thisCount] + "</b>"); 
       }); 
      } 
     }; 
    } 

我的問題是謂我不是能從其他部分,雖然其可訪問從if塊訪問array[thisCount] ..即alert(array[thisCount] + " not found");工作 請幫助關閉在JavaScript

+0

你可以嘗試在else分支中放置一個警告 - 我懷疑應該工作正常,這意味着問題是addListener方法正在做什麼。 – Xhalent 2011-05-24 10:59:28

回答

0

它是不可訪問在其他塊或「點擊」處理程序?如果你不能只在你的「點擊」處理程序中獲得array/thisCount,你是否嘗試複製這些變量?它可能是一個與上下文有關的問題嗎?試試這個,如果你的陣列其他塊內可見:

function makeTheFunction(array, thisCount) { 
    return function (point) { 
     if (!point) { 
      alert(array[thisCount] + " not found"); 
     } 


     else { 
      var item = array[thisCount]; 
      var marker = new GMarker(point); 
      map.addOverlay(marker); 
      GEvent.addListener(marker, "click", function() { 
       marker.openInfoWindowHtml(item + "</b>"); 
      }); 
     } 
    }; 

}

+0

謝謝..其工作:) :) – scooby 2011-05-24 11:10:31

+0

不用擔心,隊友:) – oddy 2011-05-24 11:14:26

0
function makeTheFunction(array, thisCount) 
{   
      if (!point) 
      { 
       alert(array[thisCount] + " not found"); 
      } 
      else 
      { 
       var marker = new GMarker(point); 
       map.addOverlay(marker); 
       GEvent.addListener(marker, "click", function() { 
        marker.openInfoWindowHtml(array[thisCount] + "</b>"); 
       }); 
      } 
     return point; 
    } 
+0

這和他給的功能不一樣。 – 2011-05-24 10:43:41

+0

它不能正常工作。它甚至不顯示警報。 :( – scooby 2011-05-24 10:43:47

0

我認爲有什麼東西在addListener功能走錯了。在提供的代碼中,偵聽器中的thisCount應該在makeTheFunction函數中關閉thisCount。

下模擬張貼代碼:

<script type="text/javascript"> 

function init() { 
    var count = 'the count'; 
    partTwo(makeFn(count)); 

    function makeFn(thisCount) { 
    return function() { 

     // Shows 'thisCount: the count' 
     alert('thisCount: ' + thisCount); 
     document.getElementById('btn0').addEventListener('click', 
     function(){alert('thisCount: ' + thisCount);}, false); 
    } 
    } 
} 

function partTwo(fn) { 
    fn(); 
} 

window.onload = function() { 
    init(); 
}; 

</script> 

<!-- Shows 'thisCount: the count' --> 
<button id="btn0">Button 0</button> 

但是它重視使用瀏覽器addEventListener,沒有明顯的定製addListener聽衆。