gpt4 book ai didi

javascript - 在按钮上添加事件监听器 - Javascript

转载 作者:太空宇宙 更新时间:2023-11-04 16:00:44 24 4
gpt4 key购买 nike

我正在尝试在动态添加到谷歌地图信息窗口的按钮上添加事件监听器。

  var contentString = '<br></br>' +
'<button type="button" class="btn btn-success" id="btnDirection">Get direction</button> </div>' +
'<button type="button" class="btn btn-success" id="btnDiscount">Related discount</button> </div>';

var marker = new google.maps.Marker({
map: map,
animation: google.maps.Animation.DROP,
position: place.geometry.location,
icon: './Google_Maps_Markers/darkgreen_MarkerK.png'
});

google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(contentString);
infoWindow.open(map, this);

var btn = $(contentString).filter('#btnDirection');
if(btn != null){
for(var i=0; i<btn.length; i++){
btn[i].addEventListener('click', function()
{ console.log("hello") });
};
};
});

每个信息窗口上都会显示按钮,但是当我点击时什么也没有发生。

有人可以帮我解决这个问题吗?

最佳答案

infowindow的内容是异步添加到DOM的,所以直到InfoWindow才找到它“domready”事件触发。

来自that documentation ):

domready | Arguments: None
This event is fired when the containing the InfoWindow's content is attached to the DOM. You may wish to monitor this event if you are building out your info window content dynamically.

要将事件监听器附加到这些按钮,请运行 JQuery 代码以在“domready”事件监听器中查找它们:

google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(contentString);
infoWindow.open(map, this);
// wait for the infowindow's domready event
google.maps.event.addListenerOnce(infoWindow, 'domready', function() {
// find the elements with the "btn" class and add the click listener to the one with id="btnDirection"
var btn = $(".btn").filter('#btnDirection');
if (btn != null) {
for (var i = 0; i < btn.length; i++) {
btn[i].addEventListener('click', function() {
console.log("hello")
});
};
};
});
});

proof of concept fiddle

代码片段:

var geocoder;
var map;

function initialize() {
var map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infoWindow = new google.maps.InfoWindow();
var contentString = '<div id="other"></div><br></br>' +
'<button type="button" class="btn btn-success" id="btnDirection">Get direction</button> </div>' +
'<button type="button" class="btn btn-success" id="btnDiscount">Related discount</button> </div>';

var marker = new google.maps.Marker({
map: map,
animation: google.maps.Animation.DROP,
position: map.getCenter(), // place.geometry.location,
});

google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(contentString);
infoWindow.open(map, this);
// wait for the infowindow's domready event
google.maps.event.addListenerOnce(infoWindow, 'domready', function() {
// find the elements with the "btn" class and add the click listener to the one with id="btnDirection"
var btn = $(".btn").filter('#btnDirection');
if (btn != null) {
for (var i = 0; i < btn.length; i++) {
btn[i].addEventListener('click', function() {
document.getElementById("other").innerHTML = "Get Direction was CLICKED";
console.log("hello")
});
};
};
});
});
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>

关于javascript - 在按钮上添加事件监听器 - Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42304417/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com