Attach event handler to element inside google maps info bubble

心不动则不痛 提交于 2019-11-30 14:51:56

You are trying to add "click" event on element ,which is not DOM Element. $('.bubble-details-button') is not DOM Element (it is a wrapper of DOM Element), but $('.bubble-details-button')[0] is.

On the other hand, when you are trying to add "click" event, the content is not created yet. You can work(e.g. add events) with content's elements ,only when they are already created. The InfoBubble will trigger "domready" event, when its content will be created.So you need to handle it:

 if (!bubble.isOpen()) {
    google.maps.event.addListenerOnce(bubble, 'domready', function(){
        google.maps.event.addDomListener($(bubble.getContent()).find('.bubble-details-button')[0], 'click', function(){
            alert("hi");
        });
    });
    bubble.open(map, mymarker);        
}

In scenarios where I have to do what you are describing, I include a JavaScript function call directly in the InfoBubble content. I often include hyperlinks within an InfoBubble, so in that case I do the following: 1 - Write a JavaScript function to handle a hyperlink click. 2 - Create the marker. 3 - Attach a click event handler to the marker that opens an InfoBubble. 4 - Define the content of the InfoBubble so that JavaScript embedded directly in the InfoBubble content is set to handle the click event by calling the JavaScript function that was defined in Step #1 - something like:

"<span>" +
     "<a href='javascript:showDetail(" + param1 + "," + param2 + ");'>" + 
      displayTextContent + "</a>" +
"</span>"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!