英文:
Different actions for clicking on Leaflet layer vs map
问题
以下是翻译好的部分:
"I've got a click event for the main part of my leaflet map. But if the user clicks on a specific layer I want to do something else and not the other action. I thought I could stop the propagation of the map click using this answer
map.originalEvent.preventDefault()
But I still get both events.
Here's a cutting from my code
this.map.on('click', (event) => {
console.log('map clicked')
})
this.boundaryLayer = L.geoJSON().addTo(this.map);
this.boundaryLayer.on('click', (event) => {
console.log('boundary clicked', event);
event.originalEvent.preventDefault();
});
英文:
I've got a click event for the main part of my leaflet map. But if the user clicks on a specific layer I want to do something else and not the other action. I thought I could stop the propagation of the map click using this answer
> map.originalEvent.preventDefault()
But I still get both events.
Here's a cutting from my code
this.map.on('click', (event) => {
console.log('map clicked')
})
this.boundaryLayer = L.geoJSON().addTo(this.map);
this.boundaryLayer.on('click', (event) => {
console.log('boundary clicked', event);
event.originalEvent.preventDefault();
});
答案1
得分: 0
这段代码阻止了地图的点击事件被触发:
this.boundaryLayer.on('click', (event) => {
console.log('boundary clicked', event);
L.DomEvent.stopPropagation(event);
});
英文:
This code stops the map's click event from being fired
this.boundaryLayer.on('click', (event) => {
console.log('boundary clicked', event);
L.DomEvent.stopPropagation(event);
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论