Trigger click on leaflet marker(触发点击传单标记)
问题描述
我在地图上有一堆传单标记.每个标记都保存在数组 markers
中.标记是动态创建的(在 ajax 调用期间).
I have a bunch of leaflet markers on the map. Each marker is held in array markers
. The markers are created dynamically (during an ajax call).
每个标记都有一个对应于特定 div 的 id(存储在 div 上的 data-mess_id
中).计划是在点击提要中相应的 div 时更改标记的图标.
Each marker has an id corresponding to a particular div (stored in data-mess_id
on the div). The plan is to change the marker's icon when its corresponding div in the feed is clicked on.
除了最后一点,一切都很好.我只是无法在标记上触发点击事件.我肯定有正确的标记,因为 baz[0].getLatLng()
正在工作.但是 baz[0].trigger('click')
不起作用.
It's all working fine except for the final bit. I just can't trigger a click event on the marker. I definitely have the correct marker because baz[0].getLatLng()
is working. But baz[0].trigger('click')
doesn't work.
我尝试动态创建一个新图标 (myHoverIcon
) 但它不起作用.
I tried creating a new icon dynamically (myHoverIcon
) but it doesn't work.
如何在标记上触发点击事件?
How do I trigger a click event on the marker?
还有其他方法可以更改标记图标吗?
Is there another way to change the marker icon?
推荐答案
要模拟鼠标点击,您可以使用 fire
方法(继承自 Evented.fire
) 在标记上:
To emulate a mouse click, you can use the fire
method (inherited from Evented.fire
) on the marker :
还有一个演示
这篇关于触发点击传单标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!