未捕获 Google 地图信息窗口中的点击事件

发布于 2024-08-20 04:56:13 字数 232 浏览 12 评论 0原文

使用 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 技术交流群。

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

发布评论

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

评论(6

捶死心动 2024-08-27 04:56:13

我无法像 Kevin Gorski 解释的那样工作......

使用 jquery 1.9.1 和maps api v=3.exp 可以进行以下工作:

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

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");
    });
});
不气馁 2024-08-27 04:56:13

如果在调用 openInfoWindowHtml 之前调用事件绑定调用(如示例所示),则当第一次调用查找具有“foo”类的元素时,跨度不在 DOM 中,因此没有附加处理程序。

您可以将该事件处理程序移动到 openInfoWindowHtml 之后调用,或者使用“实时”事件绑定,以便 jQuery 将监视 DOM 中是否有给定选择器的任何新元素。

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

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);
烛影斜 2024-08-27 04:56:13

据我所知,GMaps 以编程方式将内容注入到 InfoWindow 中,因此除非您使用事件委托,否则注入元素上的任何绑定事件处理程序都不会触发:

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

请参阅 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:

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

See the live event handlers.

失与倦" 2024-08-27 04:56:13

试试这个:

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);
    });
});

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);
    });
});
柒七 2024-08-27 04:56:13

简单的解决方案,为我工作。在span中使用onclick事件。

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

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

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

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

function test(){
alert('test OK');
}
蓝梦月影 2024-08-27 04:56:13

最简单且完整描述的解决方案。

infoWindow本身应该只包含一个具有唯一id的占位符div:

    <InfoWindow
        marker={this.state.activeMarker}
        visible={this.state.showingInfoWindow}
        onOpen={e => {
          this.onInfoWindowOpen(this.props, e);
        }}
      >
        <div id="xyz" />
      </InfoWindow>

并且在Mapcontainer< /strong>,您定义一个 onInfoWindowOpen 回调,该回调使用 onClick 事件插入单个组件/容器并将其呈现给占位符 div

    onInfoWindowOpen = () => {
        const button = (<button 
        onClick={
            () => {
            this.viewClinic(this.state.selectedPlace.slug)
            }
            }>
            View Details
        </button>);
        ReactDOM.render(React.Children.only(button),document.getElementById("xyz"));
        }


这是一个工作示例:

< 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:

    <InfoWindow
        marker={this.state.activeMarker}
        visible={this.state.showingInfoWindow}
        onOpen={e => {
          this.onInfoWindowOpen(this.props, e);
        }}
      >
        <div id="xyz" />
      </InfoWindow>

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:

    onInfoWindowOpen = () => {
        const button = (<button 
        onClick={
            () => {
            this.viewClinic(this.state.selectedPlace.slug)
            }
            }>
            View Details
        </button>);
        ReactDOM.render(React.Children.only(button),document.getElementById("xyz"));
        }


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.

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