我有一堆標記,並希望爲它們中的每一個添加一個mouseover處理程序。我通過座標循環,創建新的標記,然後添加處理程序。在處理程序中,我希望它使用特定的id修改DOM元素。在谷歌地圖api v3的循環中添加標記偵聽器?
問題是,即使在循環中id通過每次迭代都在變化,應用的實際處理程序全部使用最後生成的postId。
for(i in results)
{
r=results[i][0];
var postId=results[i][1]; // Different for each i
if (status == google.maps.GeocoderStatus.OK)
{
markers.push(new google.maps.Marker({
map: map,
position: r[0].geometry.location
}));
console.log(postId);
google.maps.event.addListener(markers[markers.length-1], 'mouseover', function() {
console.log(postId);
});
}
}
問題是無論我將鼠標懸停在哪個標記上,postId都被打印爲「1」。
但是,當我正在經歷循環時,postId每次都是不同的。
控制檯輸出:
21
20
12
10
9
3
2
1
然而,當我將鼠標懸停在標記它總是說1
事件中的警報(postID)是否具有相同的問題? – Oliver 2011-05-30 22:54:56