[英]How to Access a Div inside Google Maps InfoBox?
我正在尝试使用jquery的$('#divname')
在信息框中选择一个<div>
,当点击地图上的任何标记时,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 = '<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>
从我看到的,你是在循环中添加标记? 因此,您为每个标记的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'));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.