javascript – 点击圈子 – 传单时启动一个功能

我在JS中做了一些圈子如下:

L.circle(
  [46.765735535841024, 23.58344078063965], 5, {
    color: "blue"
  }).addTo(map).bindPopup("Description: This is my description");

我想用一个函数替换bindPopup.当我点击圆圈而不是我的描述显示时,我想运行一个函数,例如我创建了这个函数:

function circleClick() {
     // my implementations;
}

有人会告诉我怎么可能这样做?

解决方法:

只需将circleClick功能指定为每个圈子的听众:

L.circle(
  [46.765735535841024, 23.58344078063965], 5, {
    color: "blue"
  }
).addTo(map).on("click", circleClick);
// more L.circle's...

function circleClick(e) {
    var clickedCircle = e.target;

  // do something, like:
  clickedCircle.bindPopup("some content").openPopup();
}

或者,您可以在Feature Group中收集所有圈子,并将事件监听器仅附加到该组:

var group = L.featureGroup().addTo(map);

L.circle(
  [46.765735535841024, 23.58344078063965], 5, {
    color: "blue"
  }
).addTo(group);
// more L.circle's...

group.on("click", function (e) {
    var clickedCircle = e.layer; // e.target is the group itself.

  // do something, like:
  clickedCircle.bindPopup("some content").openPopup();
});
上一篇:leaflet antvPath示例


下一篇:[算法导论]强连通分量 @ Python