Attaching 'onclick' event to D3 chart background

后端 未结 2 1371
旧巷少年郎
旧巷少年郎 2021-01-31 13:54

I have a D3 histogram chart, onto which I have attached an \'onclick\' event to the bars:

...
var bar = svg.selectAll(\".bar\")
        .data(data)
        .ente         


        
2条回答
  •  -上瘾入骨i
    2021-01-31 14:32

    The event isn't actually overridden, but both are triggered -- the onclick handler for the SVG and for the bar. To prevent this, use the .stopPropagation() method (see the documentation). The code looks like this:

    rect.on("click", function() {
      console.log("rect");
      d3.event.stopPropagation();
    });
    

    Complete example here. Compare with the behaviour with stopping the propagation of the event here.

提交回复
热议问题