gpt4 book ai didi

javascript - 在传单中单击标记时不生成 map 单击事件

转载 作者:行者123 更新时间:2023-12-01 21:48:19 25 4
gpt4 key购买 nike

所以,我有一张传单 map ...

var map = L.map('map').setView([35.772219, -78.675272], 17);
map.on('click', function(e) {alert('map click!')});

然后我添加一个标记...

var marker = L.circleMarker([35.772219, -78.675272]);
marker.on('click', function(e) {alert('marker click!')});
marker.addTo(map);

如果我点击标记,标记和 map 点击事件都会触发,但我只想要标记的点击事件...有什么办法可以实现吗?我似乎在文档中找不到。

最佳答案

L.DomEvent.stopPropagation应该做的伎俩:

Stop the given event from propagation to parent elements. Used inside the listener functions:

marker.on('click', function(e) {
L.DomEvent.stopPropagation(e);
console.log('marker click!')
});

还有一个演示(一个圆圈取消了它的事件,另一个没有)

var map = L.map('map').setView([35.772219, -78.675272], 10);
map.on('click', function(e) {console.log('map click!')});


var marker = L.circleMarker([35.772219, -78.68]);
marker.on('click', function(e) {
L.DomEvent.stopPropagation(e);
console.log('marker click!')
});
marker.addTo(map);

marker = L.circleMarker([35.772219, -78.63], {fillColor: 'red'});
marker.on('click', function(e) {
console.log('marker click with map click')
});
marker.addTo(map);
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.5.1/dist/leaflet.css" />

<script src="https://unpkg.com/leaflet@1.5.1/dist/leaflet.js"></script>

<div id="map" style="height: 100px"></div>

关于javascript - 在传单中单击标记时不生成 map 单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59970774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com