2017-08-17 48 views
0

我想点击一个圆圈并收到我点击的元素。但我无法执行此触发器。我该如何解决它?触发点击当我点击一个圆圈

https://plnkr.co/edit/keLfbZ13wz8h9nMHp8pN?p=preview

var coordenadas=map.latLngToLayerPoint([coordinates[0].lat,coordinates[0].long]); 
svg.append('circle').attr("cx",coordenadas.x) 
.attr("cy", coordenadas.y) 
.attr("r", 30) 
.style("fill",'red') 
.attr("class",'circulo_mapa') 
.on("click", function(element){ 
    console.log('this is the element', element); 
    alert("click") 
}) 

回答

1

forked your plunk补充两点:

  • 定界反应拦截指针事件
  • 日志this而不是element在点击回调(这是你会可能要选择)

这里是上行方法:

var coordenadas = map.latLngToLayerPoint([coordinates[0].lat,coordinates[0].long]); 

//add circle on map 
     var coordenadas=map.latLngToLayerPoint([coordinates[0].lat,coordinates[0].long]); 
     svg.selectAll('circle').data(new Array(3)) 
      .enter() 
      .append('circle') 
      .attr("cx",function(d,i){return coordenadas.x+i*10}) 
      .attr("cy", function(d,i){return coordenadas.y+i*20}) 
      .attr("r", 30) 
      .style("fill",'red') 
      .style('stroke', 'black') 
      .style('pointer-events', 'all') 
      .style('cursor', 'pointer') 
      .attr("class",'circulo_mapa') 
      .on("click", function(d,i){ 
       console.log('this is the element', this); 
       alert("click on " + i) 
      }) 
+0

最后一个问题。我怎么知道我点击的元素的索引?例如,我有3个圈子。我点击了第二个圈子。我怎样才能让它返回1? – yavg

+0

我在plunk中只看到一个,但当绑定到数据时,回调函数有两个参数(即'function(d,i)')。第一个是绑定数据,第二个是索引 –

+0

假设我有3个圆圈。我点击其中一个。我如何获得圈子的索引?有三个圆圈,但如果我点击第三个,我需要收到数字2.我该怎么做?你懂我的? – yavg