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
1 Answer
Reset to default 6You'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);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745100091a4611220.html
评论列表(0条)