Javascript 单击圆时触发单击

Javascript 单击圆时触发单击,javascript,d3.js,Javascript,D3.js,我试图单击一个圆并接收我单击的元素。但是我不能执行这个触发器。我怎样才能修好它 我想补充两点: 使圆对截取指针事件作出反应 在单击回调中记录此而不是元素(这可能是您想要选择的) 以下是升级的方法: var coordenadas = map.latLngToLayerPoint([coordinates[0].lat,coordinates[0].long]); //add circle on map var coordenadas=map.latLngToLayerPo

我试图单击一个圆并接收我单击的元素。但是我不能执行这个触发器。我怎样才能修好它

我想补充两点:

  • 使圆对截取指针事件作出反应
  • 在单击回调中记录
    而不是
    元素
    (这可能是您想要选择的)
以下是升级的方法:

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)
            })

最后一个问题。如何知道单击的元素的索引是什么?例如,我有3个圆。我点击了第二个圆圈。如何使其返回1?我在plunk中只看到一个,但是当绑定到数据时,回调会使用两个参数(即
函数(d,I)
)。第一个是绑定数据,第二个是索引suppose,我有3个圆。我点击其中一个。如何获取圆的索引?有3个圆圈,但如果我点击第三个,我需要收到数字2。我怎么做?。你明白我的意思吗?是的,我明白了。如果您想依靠d3来帮助您,则需要使用enter update delete模式附加圆。这样,您就可以使用('click',function(d,i)),其中我对应于圆圈的索引。我还没有理解自己的意思。我这样做是为了让你能理解我。但首先看看这张照片。有三个圆圈。当我单击一个圆时,我想知道该圆在DOM中所占的索引。例如,如果我点击第三个圆圈,我需要得到一个2。你能解释一下怎么做吗?(有3个圆圈重叠,但按逻辑,单击一个圆圈就是单击最后一个)
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)
            })