如何访问Google Maps InfoBox中的Div?
我正在尝试使用jquery的$('#divname')
在信息框中选择一个
,当地图上的任何标记被点击时,Google地图会生成该信息框。
问题:当我在点击标记后尝试使用$("#gallery")
访问div时,也就是在该标记的click事件监听器内部,没有任何反应。 我尝试使用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 = ' \ \ $' + json[i].price + ' \ ' + json[i].bedroom + 'br \ ' + json[i].address_1 + ' \ \ \ \ \ \ '; 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); //
从我看到的,你是在循环中添加标记? 因此,您为每个标记的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')); });