Click event in Google Map InfoWindow not caught

跟風遠走 提交于 2019-12-04 11:47:09

问题


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 ?


回答1:


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.

$(".foo").live('click', myFunction);



回答2:


I couldnt get it working like Kevin Gorski explained...

with jquery 1.9.1 and maps api v=3.exp the following works:

infowindow.setContent('<a href="#" id="test">test</a>');
google.maps.event.addDomListener(infowindow, 'domready', function() {
    $('#test').click(function() {
        alert("Hello World");
    });
});



回答3:


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:

$(".foo").live("click", myFunction);

See the live event handlers.




回答4:


Try this:

google.maps.event.addListener(infowindow,"**domready**",function() {
    var Cancel = document.getElementById("Cancel");
    var Ok = document.getElementById("Ok");

    google.maps.event.addDomListener(Cancel,"click",function() {
        infowindow.close();
    });

    google.maps.event.addDomListener(Ok,"click",function() {
        infowindow.close();
        console.log(position);
        codeLatLng(position);
    });
});



回答5:


simple solution and work for me. use onclick event in span.

<span class=\"foo\" onclick="test()">myText</span>

function test(){
alert('test OK');
}


来源:https://stackoverflow.com/questions/2217267/click-event-in-google-map-infowindow-not-caught

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!