javascript 无法绑定到传单弹出窗口上的点击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11263243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:35:15  来源:igfitidea点击:

Unable to bind to click event on Leaflet popup

javascriptjqueryclickleaflet

提问by Radamanf

I'm using Leaflet to draw a map, within the popups I've added a link that should lead to a more detailed description, the description is separated from map and arranged into list using an accordion, so every description is hidden.

我正在使用 Leaflet 绘制地图,在弹出窗口中我添加了一个链接,该链接应该指向更详细的描述,描述与地图分离并使用手风琴排列到列表中,因此每个描述都被隐藏。

I can use an anchor to link to the accordion content, but I need to execute some JavaScript onclickso I am trying to add a clickevent handler - its not working.

我可以使用锚链接到手风琴内容,但我需要执行一些 JavaScript,onclick所以我试图添加一个click事件处理程序 - 它不起作用。

Here is my code:

这是我的代码:

<!DOCTYPE html>
<html>
<head>
    <title>Leaflet Quick Start Guide Example</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!--<script src="http://leaflet.cloudmade.com/dist/leaflet.js"></script>-->
    <script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>

    <link rel="stylesheet" href="http://code.leafletjs.com/leaflet-0.3.1/leaflet.css" />
    <!--[if lte IE 8]><link rel="stylesheet" href="http://code.leafletjs.com/leaflet-0.3.1/leaflet.ie.css" /><![endif]-->

</head>
<body>

    <div id="map" style="width: 600px; height: 400px"></div>
    <div id="log"></div>

    <div id="map_box_text" class="status_half">
            <br>
            <br>
        <p>List made using JQuery UI accordion, every element is hidden, only on hover it opens, you can click on it to display on map, reverse cliking on map should aopen accordion list description (this), it's currently done using permalinks because I cannot catch click event on a or span tag.
        <div class="accordion">
            <h4><a href="#" class="pointpopup" id="point_111_11"> Ioff :: **** ***</a></h4>
                <div>Detailed data</div>
            <br>
            <br>
            <h4><a href="#" class="pointpopup" id="point_222_22">Us sb  :: **** ***    </a></h4>
                <div>Detailed data</div>
            <br>
            <br>
            <h4><a href="#" class="pointpopup" id="point_333_33">Ioff  :: **** ***</a></h4>
                <div>Detailed data</div>
            <br>
            <br>
            <h4><a href="#" class="pointpopup" id="point_555_44">Us sb  :: **** ***    </a></h4>
                <div>Detailed data</div>
        </div>
    </div>

    <script src="http://leaflet.cloudmade.com/dist/leaflet.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){

            var map = new L.Map('map', {
                center: new L.LatLng(51.641485,-0.15362),
                zoom: 13
            });

            var cloudmadeUrl = 'http://{s}.tile.cloudmade.com/a0ead8ee56bd415896e0c7f7d22e8b6e/997/256/{z}/{x}/{y}.png',
            cloudmadeAttrib = 'Map data &copy; 2011 OpenStreetMap contributors',
            cloudmade = new L.TileLayer(cloudmadeUrl, {maxZoom: 18, attribution: cloudmadeAttrib});
            map.addLayer(cloudmade);
            var point = {};

                point["point_111_11"] = new L.Marker(new L.LatLng(51.4800166666667,-0.43673)).bindPopup("<a href=\"#point_111_11\" class=\"map_popup link\">Ioff</a> <br>**** ***");
                point["point_222_22"] = new L.Marker(new L.LatLng(51.6616333333333,-0.0528583333333333)).bindPopup("<a href=\"#point_222_22\" class=\"map_popup link\">Us sb</a> <br>**** ***");
                point["point_333_33"] = new L.Marker(new L.LatLng(52.3910783333333,-0.696951666666667)).bindPopup("<a href=\"#point_333_33\" class=\"map_popup link\">Ioff</a> <br>**** ***");
                point["point_555_44"] = new L.Marker(new L.LatLng(51.641485,-0.15362)).bindPopup("<a href=\"#point_555_44\" class=\"map_popup link\">Us sb</a> <br>**** ***");

        var points_layer = new L.LayerGroup();

                points_layer.addLayer(point["point_111_11"]);
                points_layer.addLayer(point["point_222_22"]);
                points_layer.addLayer(point["point_333_33"]);
                points_layer.addLayer(point["point_555_44"]);
        map.addLayer(points_layer);

        $('.pointpopup').click(function(){
            var pointname = this.id;
            map.setView(point[pointname].getLatLng(),15);
            point[pointname].openPopup();
        });
    });

    $(window).load(function(){

        $("body").click(function(event) {
            //console.log('event target is:' + event.target.nodeName);
            $("#log").html("clicked: " + event.target.nodeName);
        });

        $('.map_popup').live('click',function () {
        //$('.map_popup').click(function(){
            alert('Try to open Accordion ' + $(this).attr('href'))
            //console.log('Try to open Accordion');
        })
    })
    </script>
</body>
</html>

you can check it on JS Fiddle

你可以在JS Fiddle上查看

I've reported this as a bug on github to developer of Leaflet hereand herebut he close bug replying that it's not a issue and I can use another class- which doesn't work.

我已将此作为 github 上的错误报告给 Leaflet 的开发人员herehere,但他关闭了错误回复说这不是问题,我可以使用另一个class- 这不起作用。

Edit: I've found some on my own too: http://jsfiddle.net/M5Ntr/12/But there is still a problem, potentially there can be a 500 points, so I would like to have as less code as possible, I've tried to create function but I cannot pass variables :(

编辑:我自己也找到了一些:http: //jsfiddle.net/M5Ntr/12/但仍然有问题,可能有 500 分,所以我希望代码尽可能少,我试图创建函数,但我不能传递变量:(

this is working

这是工作

point["point_111_11"] = new L.Marker(new L.LatLng(51.4800166666667,-0.43673)).bindPopup("<b>Ioff</b> <br>**** ***").on('click', function (e) { console.log("clicked (Try to open Accordion): " + e.target) });

but this is preferable (not working):

但这更可取(不起作用):

point["point_111_11"] = new L.Marker(new L.LatLng(51.4800166666667,-0.43673)).bindPopup("<b>Ioff</b> <br>**** ***").on('click', myfunction('point_111_11'));

function myfunction(seclectedId){
   //do something with seclectedId
   console.log(seclectedId)
}

or even

甚至

point["point_111_11"] = new L.Marker(new L.LatLng(51.4800166666667,-0.43673)).bindPopup("<b>Ioff</b> <br>**** ***").myBindFunction('point_111_11')

which will do .on('click') or something similar inside ...

这将做 .on('click') 或类似的东西......

采纳答案by Manse

As specified in the ticket you raisedyou can create DOM elements and pass them to the bindPopupmethod ... so you can do this :

按照您提出票证中的规定,您可以创建 DOM 元素并将它们传递给bindPopup方法……因此您可以这样做:

var domelem = document.createElement('a');
domelem.href = "#point_555_444";
domelem.innerHTML = "Click me";
domelem.onclick = function() {
    alert(this.href);
    // do whatever else you want to do - open accordion etc
};

point["point_555_44"] = new L.Marker(new L.LatLng(51.641485, -0.15362)).bindPopup(domelem);

You just need to update the onclickfunction to do what you need it to do ....

你只需要更新onclick函数来做你需要它做的事情......

Here is the above section of code within your example

这是您示例中的上述代码部分