2011-10-22 29 views
3

我試圖選擇一個<div>使用jquery的$('#divname')在Google地圖生成的信息框中,當地圖上的任何多個標記點擊時。如何在Google Maps InfoBox中訪問Div?

問題:當我嘗試訪問使用$("#gallery")的div後點擊標記,即在該標記的點擊事件偵聽器內部沒有任何反應。我嘗試使用console.log($("#gallery").html())從click監聽器獲取該div中包含的html,並返回null!我怎樣才能選擇這個div!?

點擊事件監聽器代碼段

// Create Listeners 
markers[i]._index = i; 
google.maps.event.addListener(markers[i], 'click', function() { 

    infoboxes[this._index].open(map, markers[this._index]); 
    infoboxes[this._index].show(); 
    console.log($('#gallery').html()); 

});

信息框代碼片段

for(i = 0; i < json.length; i++) { 

    // Place markers on map 
    var latLng = new google.maps.LatLng(json[i].lat, json[i].lng); 
    var marker = new google.maps.Marker({ 
     position: latLng, 
     map: map 
    }); 
    markers.push(marker); 

    // Create infowindows 
    var boxText = '<div id="infobox"> \ 
         <div id="infobox_header_title"> \ 
          <span id="infobox_header_price">$' + json[i].price + '</span> \ 
          <span id="infobox_header_room">' + json[i].bedroom + 'br </span> \ 
          <span id="infobox_header_address">' + json[i].address_1 + '</span> \ 
         </div> \ 
         <div id="gallery"> \ 
          <img src="images/cl/' + json[i].photos[0] + '.jpg" /> \ 
          <img src="images/cl/' + json[i].photos[1] + '.jpg" /> \ 
         </div> \ 
        </div>'; 
    var infoboxOptions = { 
     content: boxText, 
     disableAutoPan: false, 
     maxWidth: 0, 
     pixelOffset: new google.maps.Size(-140, 0), 
     zIndex: null, 
     boxStyle: { 
     }, 
     closeBoxMargin: "10px 2px 2px 2px", 
     closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif", 
     infoBoxClearance: new google.maps.Size(1, 1), 
     isHidden: true, 
     pane: "floatPane", 
     enableEventPropagation: false 
     }; 

    var infobox = new InfoBox(infoboxOptions); 
    infoboxes.push(infobox); 

      //<Click Event Listener code comes in here> 

回答

1

從我所看到的,你是在一個循環中添加標記?因此,對每個標記的infoWindow使用相同的ID,並且違反HTML/JS/CSS規則,使其具有相同ID的一個元素,並且這是正常的失敗。我會做的是,而不是使用我的標記的類和事件監聽器,你可以得到像這樣的.gallery div的內容(還沒有嘗試,但你應該明白,如果它不起作用):

// Create Listeners 
markers[i]._index = i; 
google.maps.event.addListener(markers[i], 'click', function() { 

    infoboxes[this._index].open(map, markers[this._index]); 
    infoboxes[this._index].show(); 
    console.log($(infoboxes[this._index].getContent()).find('.gallery')); 

}); 
相關問題