未捕获 Google 地图信息窗口中的点击事件
使用 Google Map v2,我希望能够在单击 GMarker 信息窗口中的文本时触发功能。
$(".foo").click(myFunction);
...
marker.openInfoWindowHtml("<span class=\"foo\">myText</span>");
不起作用。为什么该事件没有被捕获在 InfoWindow 内?
With Google Map v2, I would like to be able to trigger a function when clicking a text in the InfoWindow of a GMarker.
$(".foo").click(myFunction);
...
marker.openInfoWindowHtml("<span class=\"foo\">myText</span>");
does not work. Why the event isn't caught inside the InfoWindow ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我无法像 Kevin Gorski 解释的那样工作......
使用 jquery 1.9.1 和maps api v=3.exp 可以进行以下工作:
I couldnt get it working like Kevin Gorski explained...
with jquery 1.9.1 and maps api v=3.exp the following works:
如果在调用 openInfoWindowHtml 之前调用事件绑定调用(如示例所示),则当第一次调用查找具有“foo”类的元素时,跨度不在 DOM 中,因此没有附加处理程序。
您可以将该事件处理程序移动到 openInfoWindowHtml 之后调用,或者使用“实时”事件绑定,以便 jQuery 将监视 DOM 中是否有给定选择器的任何新元素。
If the event binding call is called before the call to openInfoWindowHtml as it is in your example, the span wasn't in the DOM while the first call was looking for elements with the class "foo," so no handler was attached.
You can either move that event handler to be called after openInfoWindowHtml, or use "live" event binding so that jQuery will monitor the DOM for any new elements with the given selector.
据我所知,GMaps 以编程方式将内容注入到 InfoWindow 中,因此除非您使用事件委托,否则注入元素上的任何绑定事件处理程序都不会触发:
请参阅
live
事件处理程序。As far as I know, GMaps injects content into the InfoWindow programatically, so any bound event handlers on the injected elements will not fire unless you use event delegation:
See the
live
event handlers.试试这个:
Try this:
简单的解决方案,为我工作。在span中使用onclick事件。
simple solution and work for me. use onclick event in span.
最简单且完整描述的解决方案。
infoWindow本身应该只包含一个具有唯一id的占位符div:
并且在Mapcontainer< /strong>,您定义一个 onInfoWindowOpen 回调,该回调使用 onClick 事件插入单个组件/容器并将其呈现给占位符 div:
这是一个工作示例:
< a href="https://codesandbox.io/s/k0xrxok983" rel="nofollow noreferrer">https://codesandbox.io/s/k0xrxok983
一个更完整的示例MAP 、标记和信息窗口:
https://codesandbox.io /s/24m1yrr4mj
如果您有任何疑问,请发表评论。
The simplest and fully describe solutions.
The infoWindow itself should only contain a placeholder div with a unique id:
And inside the Mapcontainer, you define an onInfoWindowOpen callback, that inserts a single component/container with the onClick event and render it to a placeholder div:
Here is a working example:
https://codesandbox.io/s/k0xrxok983
One more complete example MAP, Marker and InfoWindow:
https://codesandbox.io/s/24m1yrr4mj
If you have any questions so please comment.