首页 > 解决方案 > L.marker.bindPopup() 的 onpopupload 事件?

问题描述

假设我有一些这样的代码:

    L.marker({lat: 30.266293, lon: -97.656965}, {icon: myIcon})
        .addTo(mymap)
        .bindPopup("<a href='#' class='test'>click me!</a>");

我希望能够$('test').click(function() { ... });在弹出窗口加载后做。

我该怎么做?我可以使用某种 onpopupload 事件吗?

标签: leaflet

解决方案


是的,图层上有一个事件popupopen

var marker = L.marker({lat: 30.266293, lon: -97.656965}, {icon: myIcon})
        .addTo(mymap)
        .bindPopup("<a href='#' class='test'>click me!</a>");

marker.on('popupopen',(e)=>{
    $('.test').click(function() { ... });
});

PS:点击事件会添加到test元素中,必须在前面加一个点test,否则找不到元素--> $('.test')


推荐阅读