javascript - 关闭传单中的弹出窗口
问题描述
我有一个代码,它使用一个标记来呈现一个弹出窗口,leaflet
如下所示。
const lat = e.latlng.lat;
if ($('#lmap').css("cursor") === 'crosshair') {
const note = `<div>popup<div>`;
const newMarker = new L.marker(e.latlng,{draggable:true}).addTo(lmap);
if (!(note === "")) {
newMarker.bindPopup(note, {closeOnClick: false, autoClose: false, closeButton: true}).openPopup();
}
}
这里
closeButton: true
用于在我的弹出窗口中呈现关闭按钮。但问题是当我点击标记时弹出关闭。如何在单击标记时防止关闭弹出窗口,并在单击弹出窗口中的关闭按钮时关闭它。
解决方案
不要将弹出窗口直接添加到标记,创建一个弹出“图层”并在单击标记时打开它:
// a global variable
var popup = L.popup({closeOnClick: false, autoClose: false, closeButton: true});
//your function from above
const newMarker = new L.marker(e.latlng,{draggable:true}).addTo(lmap);
newMarker.on('click',(e)=>{
popup.options.offset = e.target.options.icon.options.popupAnchor;
popup.setContent('TEST').setLatLng(e.target.getLatLng()).addTo(map)
})
推荐阅读
- c - 在C中查找未知数组的大小
- php - 为什么 DOMDocument::saveHTML() 的行为在将 UTF-8 编码为样式和脚本元素中的实体时会有所不同?
- powershell - PowerShell Invoke-RestMethod 截断最后一个周期
- fortran - 使用 Minpack 求解 S 曲线
- ruby - 为什么我们使用 html_safe?
- react-native - React Native 在 App.js 渲染中调用另一个 .js
- google-cloud-dataflow - 使用内部 IP 的云数据流作业?
- r - 表格不会在 Shiny 中呈现
- javascript - 使用 webpack splitChunks 防止重复
- javascript - object.onsubmit 事件未激活