我正在尝试访问leaflet弹出窗口中的内容。具体来说,我已经添加了一个带有按钮的表单,我想要访问它。但现在我只是尝试为弹出窗口本身添加一个事件。
$(".leaflet-popup-content-wrapper .leaflet-popup-content").click(function(e) {
alert("clicked");
});
LeafletJs 带弹出窗口的标记示例:
<form class="popup-form">
<div class="form-group">
<label class="mb-0" for="comment">Comment:</label>
<textarea class="form-control" rows="4" class="comment">${feature.properties.note}</textarea>
</div>
<div class="d-flex">
<button type="submit" class="btn btn-outline-info btn-sm">Save</button>
<button class="delete-button btn btn-outline-danger btn-sm ml-auto">Delete</button>
</div>
</form>
设置弹出内容的代码
var points = new L.geoJson(null, {
onEachFeature: function (feature, layer) {
layer.bindPopup(feature.properties.note);
let myPopup = L.DomUtil.create('div', 'content');
content = `
<form class="popup-form">
<div class="form-group">
<label class="mb-0" for="comment">Comment:</label>
<textarea class="form-control" rows="4" class="comment">${feature.properties.id}</textarea>
</div>
<div class="d-flex">
<button type="submit" class="btn btn-outline-info btn-sm">Save</button>
<button class="delete-button btn btn-outline-danger btn-sm ml-auto">Delete</button>
</div>
</form>
`;
layer.bindPopup(content); // Create empty popup
$('#form', myPopup).on('click', function() {
alert("form clicked")
});
受此帖子启发 如何在leaflet弹出窗口上捕获单击事件
我不明白这个代码示例的“上下文”是什么?
var content = L.DomUtil.create('div', 'content'),
popup = L.popup().setContent(content);
L.DomEvent.addListener(content, 'click', function(event){
// do stuff
}, context);