Leaflet不同的悬停和点击事件

时间:2012-10-21 22:46:41

标签: jquery mouseevent bind mouseover leaflet

我正在尝试为鼠标悬停事件提供弹出窗口,而不是在标记上单击做一些其他东西(例如func)点击后

我不相信的一半成功的代码会帮助你思考这个方向:

(我只是在点击事件上添加悬停)

marker[i].on('mouseover', marker[i].bindPopup('hi').openPopup.bind(marker[i]));

[i]只代表一个循环


Leaflet的API:http://leaflet.cloudmade.com/reference.html#map-openpopup

2 个答案:

答案 0 :(得分:10)

以下代码显示标记被鼠标悬停时的弹出窗口,并在单击标记时执行其他操作:

marker[i].on('mouseover', function(evt) {
  //evt.target is the marker that is being moused over 
  //bindPopup() does not need to be called here if it was already called
  //somewhere else for this marker.
  evt.target.bindPopup('hi').openPopup();
});
marker[i].on('click', function(evt) {
  //again, evt.target will contain the marker that was clicked
  console.log('you clicked a marker');
});

答案 1 :(得分:1)

您没有为鼠标悬停事件提供回调。

marker[i].on('mouseover', function () {
    marker[i].bindPopup('hi').openPopup.bind(marker[i])
});

传入一个匿名函数作为你的回调,当它被调用时,它会做你想要的。

我对传单api不太熟悉(几个月前才开始使用它)所以marker[i].bindPopup('hi').openPopup.bind(marker[i])也可能存在问题。