Google Maps API - 无法点击信息窗口中的链接

发布于 2024-11-02 11:36:40 字数 699 浏览 0 评论 0原文

我在 Javascript 中的 GoogleMaps API v3 中遇到 InfoWindow 问题。我已经成功创建了一个信息窗口,我可以在里面设置任何内容,但我想在里面放一个超文本链接。它工作正常,我通常将标记放入 setContent() 方法,并且链接正确显示在窗口中。不幸的是,它不可点击 - 它附有一个正确的链接,但当我点击它时,什么也没有发生。当我通过右键单击它并选择“在新窗口中打开”时,它工作得很好。有人能找出这里的问题吗?我有一个简单的代码,如下所示:

var bubble;

... some lines not related to the bubble ...

bubble = new google.maps.InfoWindow({
  maxSize: new google.maps.Size(500,250)
});

... other not related lines...

然后我在创建标记的事件中触发它(对于标记来说效果很好):

bubble.setContent('<a href="http://www.google.com">LINK</a>');

google.maps.event.addListener(marker, 'click', function() {
   bubble.open(map, marker)
});

其中“地图”和“标记”都很好。

I've got a problem with InfoWindow in GoogleMaps API v3 in Javascript. I have successfully created an info window, I can set any content inside, but I want to put a hypertext link inside. It works fine, i put normally tag to the setContent() method and the link appears in the window correctly. Unfortunately, it is NOT clickable - it has a correct link attached to it, but when I click on it, nothing happens. When I click on it by right button and select "Open in new windows", it works just fine. Can anyone find out a problem here? I've got a code as simple as follows:

var bubble;

... some lines not related to the bubble ...

bubble = new google.maps.InfoWindow({
  maxSize: new google.maps.Size(500,250)
});

... other not related lines...

then I trigger it in an event that creates markers (works fine for the markers):

bubble.setContent('<a href="http://www.google.com">LINK</a>');

google.maps.event.addListener(marker, 'click', function() {
   bubble.open(map, marker)
});

where both "map" and "marker" are just fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ζ澈沫 2024-11-09 11:36:40

我遇到了同样的问题。我相信这是因为 infowindow 内容作为 div 动态插入到 DOM 中而引起的。我不知道为什么会发生这种情况,但这是我解决它的方法:

  1. 使用 jQuery (我已经在使用这个 - 如果你使用替代的 js 库,可能有相同的等效解决方案)
  2. 更新 html链接也定义了一个类(例如 class="infowindow_link")
  3. 添加 jQuery 处理程序以捕获链接并关注它

    $(".infowindow_link").live('click', function(){
    window.location.href=this.href;
    });

这个“实时”调用跟踪 DOM 的更改,并将侦听器附加到您插入的任何新链接。 window.location.href 更新是跟踪链接的正常行为。

I faced the same problem. I believe this is being caused because the infowindow content is dynamically inserted into the DOM as a div. I don't know why this exactly happens, but here is how I solved it:

  1. Use jQuery (I was already using this - if you use an alternative js lib, there may be equivalent solution(s) for the same)
  2. update the html links to also have a class defined (say class="infowindow_link")
  3. Add a jQuery handler to capture the link and follow it

    $(".infowindow_link").live('click', function(){
    window.location.href=this.href;
    });

This 'live' call tracks changes to the DOM and attaches the listeners to any new links you insert. The window.location.href update is the normal behavior of following a link.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文