添加事件处理程序在信息窗口内的元素谷歌地图v3
您好,我正在尝试添加到信息窗口内的按钮,我尝试过这样的事件
var infoWindow = new google.maps.InfoWindow({
content: '<div></br><span class="formatText">Ubicacion del Marcador: </span>' + event.latLng.toString()
+ '</br> <input type="button" id="btnPrueba" value="prueba"/></div>'
});
google.maps.event.addListener(marker, 'click', function () {
infoWindow.open(map, this);
google.maps.event.addDomListener(document.getElementById('btnPrueba'), 'click', removeMarker);
});
function removeMarker(){
alert('it works');
}
我做错了什么?或者这是另一种方法来做到这一点?谢谢
编辑
我也尝试过像这样的jquery,但是尽管它是通过函数获取的事件,但警报没有显示。当我用 chrome 进行调试时,它可以工作:(
google.maps.event.addListener(marker, 'click', function () {
infoWindow.open(map, this);
$("#btnPrueba").click(function () {
alert('funciona');
});
});
hi I'm trying to add to button inside an info window an event i tried like this
var infoWindow = new google.maps.InfoWindow({
content: '<div></br><span class="formatText">Ubicacion del Marcador: </span>' + event.latLng.toString()
+ '</br> <input type="button" id="btnPrueba" value="prueba"/></div>'
});
google.maps.event.addListener(marker, 'click', function () {
infoWindow.open(map, this);
google.maps.event.addDomListener(document.getElementById('btnPrueba'), 'click', removeMarker);
});
function removeMarker(){
alert('it works');
}
What am i doing wrong? or it's another way to do this?. Thanks
Edit
I'm also tried with jquery like this but although the event it's get by the function the alert doesn't show. When i do the debug with chrome it's works :(
google.maps.event.addListener(marker, 'click', function () {
infoWindow.open(map, this);
$("#btnPrueba").click(function () {
alert('funciona');
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试按钮的“onclick”属性。
Try the "onclick" attribute for the button.
调用像 @DeeV 所示的全局函数确实看起来是最直接的方法(除了稍微弄脏全局命名空间,这在当今被看不起)。 jQuery 事件委托确实可以在没有任何额外的 setTimeout 体操的情况下工作,但感觉就像一个更重的函数调用堆栈来完成相同的目标。例如
,但是,坚持使用全局方法,除了@DeeV的答案之外,通常的技巧是获取一些可以在该事件处理程序中执行操作的上下文值:
InfoWindow 内容字符串
它是对数组的引用(也必须是全局的),从而方便
稍后在处理程序中调用标记 API(例如 setIcon() 等),
例如 JS 伪代码
Calling a global function like @DeeV's shown really does seem like the straightest shot at this (except for dirtying the global namespace a smidge which is looked down on these days). jQuery event delegation does work without any additional setTimeout gymnastics but feels like a heavier function call stack to accomplish the same goal. e.g.
However, sticking with the global approach, on top of @DeeV's answer, often the trick is getting some contextual values that you can act upon in that event handler:
InfoWindow content string
it's reference to an array (also necessarily global) and thereby facilitate
Marker API calls later in the handler (e.g. setIcon(), etc.)
e.g. JS Pseudocode
我的解决方法是使用较短的超时。
当我不使用
setTimeout
包装器时,它不起作用。即使setContent()
和open()
已经被调用。我认为这意味着创建是异步完成的。注意:“50”是一个神奇的数字,因此可能很脆弱。
注意:使用以下内容对我来说也不起作用,我仍然觉得很奇怪并且无法解释:
My workaround was to use a short timeout.
When I didn't use the
setTimeout
wrapper it did not work. Even thoughsetContent()
andopen()
had already been called. I assume this means the creation is done asynchronously.NOTE: the "50" is a magic number, so potentially brittle.
NOTE: using the following also did not work for me, which I still find strange and cannot explain: