How can I make double click event on node in d3.js?

你说的曾经没有我的故事 提交于 2019-12-31 11:00:12

问题


I want to make double click event on nodes. So I tried

.on("dbclick",function(d){return "http://google.com");});

and

.bind({"dbclick",function(d){alert("hello")} });

But all failed. Can anyone help me?

Full codes are below.

var node = svg.selectAll(".node")
    .data(graph.nodes)
    .enter().append("g")
    .attr("class", "node")
    //.on("dbclick",function(d){return "http://google.com");});
    //.attr("xlink:href", function(d){return d.url;}
    .call(force.drag);
    //.bind({"dbclick",function(d){alert("hello")} });

Finally, I used a below method. (dblclick also works)

var node = svg.selectAll(".node") .data(graph.nodes) .enter().append("a") 
              .attr("class", "node") .attr("target", "_blank")
              .attr("xlink:href", function(d){return "google.com";;}) 

回答1:


You can use "dblclick" instead of "dbclick":

nodes.on("dblclick",function(d){ alert("node was double clicked"); });



回答2:


If using D3 Observable:

const nodeEnter = node.enter().append("g")
        .on("dblclick", d => {
          d3.event.preventDefault();
          // do your thing
        });


来源:https://stackoverflow.com/questions/20031254/how-can-i-make-double-click-event-on-node-in-d3-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!