我正在使用我的數據庫獲取將在Google地圖上設置爲不同位置的標記。我使用JavaScript來還爲所有這些動態加載的標誌事件偵聽器,但我有這個一個問題:動態地在Google地圖上設置活動不起作用
var markerCollection = [];
//pushing data into the array...
for (var j = 0; j < markerCollection.length; j++) {
console.log(j + ' ' + markerCollection[j].shopId);
$.proxy(google.maps.event.addListener(markerCollection[j].gmapMarker, "click", function(e) {
console.log(markerCollection[j].shopId);
}),this);
}
代碼告訴我markerCollection[j]
是不確定的。然而,如果我輸入一個硬編碼的方式(markerCollection [0]),它將起作用。爲什麼會發生這種情況,我該如何解決?
如果$ .proxy是一個異步函數,當它執行的時候它會丟失j的值,它不在範圍內 – juvian
@juvian,不使用代理會給出同樣的錯誤。 – user3141607
它會在控制檯日誌或addListener中給出錯誤嗎? – juvian