最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Bind click event to D3 element - Stack Overflow

programmeradmin1浏览0评论

The following results in the method nodeClick() being called once upon page load (without clicking). Why? How do I make the nodeClick() function be triggered only when I click the element?

Code:

var node = svg.selectAll(".node")
    .on("click", nodeClick());

in the context:

var width = 960,
   height = 500;

var color = d3.scale.category10();

var nodes = [],
    links = [];

var force = d3.layout.force()
    .nodes(nodes)
    .links(links)
    .charge(-400)
    .linkDistance(120)
    .size([width, height])
    .on("tick", tick);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

var node = svg.selectAll(".node")
    .on("click", nodeClick()),
    link = svg.selectAll(".link");

function start() {
    link = link.data(force.links(), function(d) { return d.source.id + "-" + d.target.id; });
    link.enter().insert("line", ".node").attr("class", "link");
    link.exit().remove();

    node = node.data(force.nodes(), function(d) { return d.id;});
    node.enter().append("circle").attr("class", function(d) { return "node " + d.id; }).attr("r", 8);
    node.exit().remove();

    force.start();
}

function nodeClick() {
    console.log("ASDASDASD");
}

function tick() {
    node.attr("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; })

    link.attr("x1", function(d) { return d.source.x; })
        .attr("y1", function(d) { return d.source.y; })
        .attr("x2", function(d) { return d.target.x; })
        .attr("y2", function(d) { return d.target.y; });
}

The following results in the method nodeClick() being called once upon page load (without clicking). Why? How do I make the nodeClick() function be triggered only when I click the element?

Code:

var node = svg.selectAll(".node")
    .on("click", nodeClick());

in the context:

var width = 960,
   height = 500;

var color = d3.scale.category10();

var nodes = [],
    links = [];

var force = d3.layout.force()
    .nodes(nodes)
    .links(links)
    .charge(-400)
    .linkDistance(120)
    .size([width, height])
    .on("tick", tick);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

var node = svg.selectAll(".node")
    .on("click", nodeClick()),
    link = svg.selectAll(".link");

function start() {
    link = link.data(force.links(), function(d) { return d.source.id + "-" + d.target.id; });
    link.enter().insert("line", ".node").attr("class", "link");
    link.exit().remove();

    node = node.data(force.nodes(), function(d) { return d.id;});
    node.enter().append("circle").attr("class", function(d) { return "node " + d.id; }).attr("r", 8);
    node.exit().remove();

    force.start();
}

function nodeClick() {
    console.log("ASDASDASD");
}

function tick() {
    node.attr("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; })

    link.attr("x1", function(d) { return d.source.x; })
        .attr("y1", function(d) { return d.source.y; })
        .attr("x2", function(d) { return d.target.x; })
        .attr("y2", function(d) { return d.target.y; });
}
Share Improve this question edited Jun 12, 2020 at 7:10 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Aug 15, 2014 at 9:24 user3582590user3582590 1872 gold badges4 silver badges13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You're actually calling the function when you're setting up the click handler. You need to attach a function to the handler that will call the function you actually want when called:

.on("click", function() { nodeClick() });

or equivalently give it the function name without calling the function (shorter)

.on("click", nodeClick);
发布评论

评论列表(0)

  1. 暂无评论