在 Google Map InfoWindow 中使用 jQuery Live 注册的单击事件

发布于 2024-09-16 08:13:18 字数 2166 浏览 5 评论 0原文

我正在使用此处描述的技术未捕获Google Map InfoWindow中的点击事件< /a> 将单击事件处理程序动态添加到添加到 Google 地图信息窗口的某些内容。除 IE 外,效果都很好。

我有带有普通锚标签的内容,这些链接工作得很好。

我有一个带有 jQ​​uery(实时)点击处理程序的锚标记,它读取 id 来触发一些其他操作,但它在 IE 中什么也不做。 Chrome/FF 等工作得很好。我尝试使用 div 和 span 而不改变行为。

有什么想法吗?

编辑: 这是 Google 地图 v2 的版本(我知道它已被弃用),并且在 IE 7 或 8 中不起作用。在这个项目上还没有考虑过 IE6。

编辑:以下是一些存储该行为的示例代码。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA" type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>

<style type="text/css">
#gmap{ height:450px;width:350px; }
</style>

<script type="text/javascript">
    var gmap;
    var sampleLatLon = new GLatLng(37.4419, -122.1419);
    var sampleIcon = new GIcon(G_DEFAULT_ICON);

    $(document).ready(function () {

        // Google maps setup
        $(window).unload(function () { GUnload(); });
        var gmap = new GMap2(document.getElementById("gmap"));
        gmap.setCenter(sampleLatLon, 13);
        gmap.setUIToDefault();

        // Marker and InfoWindow setup
        var marker = new GMarker(sampleLatLon, { icon: sampleIcon });
        var infoWindowMarkup = '<a id="infowindow-1" class="clickeventvialive">click me</a>';
        marker.bindInfoWindowHtml(infoWindowMarkup);
        gmap.addOverlay(marker);

        $('.clickeventvialive').live('click', function () {
            alert('Are you Internet Exploder??');
        });
    });
</script>

</head>
<body>
    <div id="gmap"></div>
</body>
</html>

I was using the technique described here Click event in Google Map InfoWindow not caught to dynamically add click event handlers to some of the content added to the Google Map InfoWindow. Works great except in IE.

I have content with normal anchor tags and those links work just fine.

I have an anchor tag with a jQuery (live) click handler read the id to trigger some other action and it does nothing in IE. Chrome/FF etc work just fine. I've tried using a div and span with no change to the behavior.

Any ideas?

Edit: This is with Google Maps v2 (I do know it is deprecated) and doesn't work in IE 7 or 8. Haven't bothered with IE6 on this project.

Edit: Here is a some sample code that repos the behavior.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="http://maps.google.com/maps?file=api&v=2&sensor=false&key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA" type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>

<style type="text/css">
#gmap{ height:450px;width:350px; }
</style>

<script type="text/javascript">
    var gmap;
    var sampleLatLon = new GLatLng(37.4419, -122.1419);
    var sampleIcon = new GIcon(G_DEFAULT_ICON);

    $(document).ready(function () {

        // Google maps setup
        $(window).unload(function () { GUnload(); });
        var gmap = new GMap2(document.getElementById("gmap"));
        gmap.setCenter(sampleLatLon, 13);
        gmap.setUIToDefault();

        // Marker and InfoWindow setup
        var marker = new GMarker(sampleLatLon, { icon: sampleIcon });
        var infoWindowMarkup = '<a id="infowindow-1" class="clickeventvialive">click me</a>';
        marker.bindInfoWindowHtml(infoWindowMarkup);
        gmap.addOverlay(marker);

        $('.clickeventvialive').live('click', function () {
            alert('Are you Internet Exploder??');
        });
    });
</script>

</head>
<body>
    <div id="gmap"></div>
</body>
</html>

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

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

发布评论

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

评论(1

爱的故事 2024-09-23 08:13:18

有两种方法可以解决这个问题,一种是使用 jquery,另一种是使用旧的 skool javascript onclick。

在 jQuery 方法中,在显示信息窗口后绑定事件并且创建的元素似乎解决了问题:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script src="http://maps.google.com/maps?file=api&v=2&sensor=false&key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA" type="text/javascript">
        </script>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript">
        </script>
        <style type="text/css">
            #gmap {
                height: 450px;
                width: 350px;
            }


        </style>
        <script type="text/javascript">
            var gmap;
            var sampleLatLon = new GLatLng(37.4419, -122.1419);
            var sampleIcon = new GIcon(G_DEFAULT_ICON);

            $(document).ready(function(){

                // Google maps setup
                $(window).unload(function(){
                    GUnload();
                });
                var gmap = new GMap2(document.getElementById("gmap"));
                gmap.setCenter(sampleLatLon, 13);
                gmap.setUIToDefault();

                // Marker and InfoWindow setup
                var marker = new GMarker(sampleLatLon, {
                    icon: sampleIcon
                });

                //marker.bindInfoWindowHtml(infoWindowMarkup);

                GEvent.addListener(marker, "click", function(){
                    var infoWindowMarkup = '<div class="container"><a id="infowindow-1" class="clickeventvialive">click me</a></div>';
                    gmap.openInfoWindowHtml(sampleLatLon, infoWindowMarkup, {
                        "onOpenFn": function(){
                         $(".clickeventvialive").bind("click", function (){alert("hello")});
                        }
                    });

                });



                gmap.addOverlay(marker);

            });
        </script>
    </head>
    <body>
        <div id="gmap">
        </div>
    </body>
</html>

或者旧的 skool 方法,您可以简单地将 onclick 处理程序添加到链接 HTML 中:

var infoWindowMarkup = '<a id="infowindow-1" onclick=alert("Old Skool") class="clickeventvialive">click me</a>';

There are two ways of solving that one with jquery another using old skool javascript onclick.

In the jQuery method binding the event AFTER the info window is shown and the element created seems to solve the problem:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script src="http://maps.google.com/maps?file=api&v=2&sensor=false&key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA" type="text/javascript">
        </script>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript">
        </script>
        <style type="text/css">
            #gmap {
                height: 450px;
                width: 350px;
            }


        </style>
        <script type="text/javascript">
            var gmap;
            var sampleLatLon = new GLatLng(37.4419, -122.1419);
            var sampleIcon = new GIcon(G_DEFAULT_ICON);

            $(document).ready(function(){

                // Google maps setup
                $(window).unload(function(){
                    GUnload();
                });
                var gmap = new GMap2(document.getElementById("gmap"));
                gmap.setCenter(sampleLatLon, 13);
                gmap.setUIToDefault();

                // Marker and InfoWindow setup
                var marker = new GMarker(sampleLatLon, {
                    icon: sampleIcon
                });

                //marker.bindInfoWindowHtml(infoWindowMarkup);

                GEvent.addListener(marker, "click", function(){
                    var infoWindowMarkup = '<div class="container"><a id="infowindow-1" class="clickeventvialive">click me</a></div>';
                    gmap.openInfoWindowHtml(sampleLatLon, infoWindowMarkup, {
                        "onOpenFn": function(){
                         $(".clickeventvialive").bind("click", function (){alert("hello")});
                        }
                    });

                });



                gmap.addOverlay(marker);

            });
        </script>
    </head>
    <body>
        <div id="gmap">
        </div>
    </body>
</html>

or the old skool method where you can simply add onclick handler to to your link HTML:

var infoWindowMarkup = '<a id="infowindow-1" onclick=alert("Old Skool") class="clickeventvialive">click me</a>';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文