5

我使用以下示例生成有向图

http://bl.ocks.org/1153292

我想添加一个单击事件,以便当用户单击某个节点时,显示该节点的标题

到目前为止,我这样做了

var circle = svg.append("svg:g").selectAll("circle")
    .data(force.nodes())
  .enter().append("svg:circle")
    .attr("r", 6)
    **.on("mouseup", disp)**
    .call(force.drag);
     ;

function disp() {
    alert("Display the heading of the node clicked here");

};

请告诉我如何显示

4

1 回答 1

4

您可以使用.on()来获得点击事件

circle.on("click", function(d) {
    alert(d.name)
})

jsFiddle:http: //jsfiddle.net/chrisJamesC/HgHqy/

于 2013-08-06T21:50:42.527 回答