简体   繁体   English

传单弹出窗口未注册点击事件

[英]Leaflet Popup not registering click events

I am trying to embed an interactive image carousel within a Leaflet popup but any content I place within the popup element does not register mouse events.我正在尝试在 Leaflet 弹出窗口中嵌入交互式图像轮播,但我放置在弹出元素中的任何内容都不会注册鼠标事件。

I created a simple test to see if I could register a simple click event onto a popup, but nothing happens.我创建了一个简单的测试,看看我是否可以在弹出窗口上注册一个简单的点击事件,但没有任何反应。 Leaflet markers register click events fine, but on popups its disabled.传单标记可以很好地注册点击事件,但在弹出窗口中它被禁用。

Why is this happening and how do I enable popup to register mouse events?为什么会发生这种情况以及如何启用弹出窗口来注册鼠标事件?

JsFiddle Example here JsFiddle 示例在这里

// create a map in the "map" div, set the view to a given place and zoom
var map = L.map('map').setView([51.505, -0.09], 13);

// add an OpenStreetMap tile layer
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
    attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

// add a marker in the given location, attach some popup content to it and open the popup
var marker = L.marker([51.5, -0.09]).addTo(map);
var popup = L.popup()
    .setContent('<p>Hello world!<br />This is a nice popup.</p>')
        .openOn(marker)
 marker.bindPopup(popup); 
 //marker registers click events
 marker.on("click", displayMarkerMessage);
 // popup does not register click events
 popup.on("click", displayPopupMessage);

 function displayMarkerMessage(){
 console.log("marker click");
 } 

 function displayPopupMessage(){
 console.log("popup click");
 }

Thanks谢谢

Add a click event on the DOM element when you open the popup.当您打开弹出窗口时,在 DOM 元素上添加一个单击事件。

 function displayMarkerMessage(){
   var popup  = document.getElementsByClassName('leaflet-popup-content-wrapper');
   if(popup != null && popup.length > 0){
     L.DomEvent.off(popup[0]); //to reset all events on the popoup, maybe else it is called twice
     L.DomEvent.on(popup[0],'click',displayPopupMessage);
   }
 } 

if you have multiple popups open at the same time, you have to loop through the popups and add the click events to them.如果您同时打开多个弹出窗口,则必须遍历弹出窗口并将单击事件添加到它们。

Also you can use the popupopen event to get the popup container and add the click event.您也可以使用popupopen事件来获取弹出容器并添加点击事件。 https://leafletjs.com/reference-1.6.0.html#popup-popupopen https://leafletjs.com/reference-1.6.0.html#popup-popupopen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM