如何在googlemaps信息窗口中检测按钮单击 [英] How to detect button click in googlemaps infowindow

查看:50
本文介绍了如何在googlemaps信息窗口中检测按钮单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Googlemaps V3

Using Googlemaps V3

我试图在我的googlemap上的infoWindow中添加一个按钮.infoWindows和按钮按预期出现.我正在尝试捕获按钮的onclick事件.这是我到目前为止的内容:

I am trying to add a button to the infoWindow on my googlemap. The infoWindows and button appear as expected. I am trying to capture the onclick event of the button. This is what I have so far:

var contentBtn = '<p><button id="marker'+numby+'" class="iwLink" href="#" >Done</button></p>';
        google.maps.event.addListener(marker, 'click', function() {
            infowindow.setContent(address+contentBtn);
            infowindow.open(map, this);
        });

        google.maps.event.addDomListener(contentBtn, 'click', function() {

          alert($(this).attr('id'));
        });

infoWindow随同按钮一起出现.但是,我在控制台日志中也收到以下错误:

The infoWindow appears as expected, along with the button. However, I also get the following error in the console log:

未捕获的TypeError:无法读取未定义的属性'click'
这似乎是由 google.maps.event.addDomListener(contentBtn,'click',function()

推荐答案

在信息窗口完成渲染之前,按钮不会添加到DOM中.您必须等到 infowindow domready之后,才能使用getElementById(或等效的jquery)找到它.事件触发.

The button isn't added to the DOM until the infowindow completes rendering. You can't find it with getElementById (or the jquery equivalent) until after the infowindow domready event fires.

var contentBtn = '<p><button id="marker'+numby+'" class="iwLink" href="#" >Done</button></p>';
    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(address+contentBtn);
        infowindow.open(map, this);
    });
    google.maps.event.addListener(infowindow, 'domready', function() {
      google.maps.event.addDomListener(contentBtn, 'click', function() {

        alert($(this).attr('id'));
      });
    });

这篇关于如何在googlemaps信息窗口中检测按钮单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆