JavaScript d3将参数传递到引用d3.event.y的函数中
我想编辑dragmove函数以接受参数。 这是原始代码:JavaScript d3将参数传递到引用d3.event.y的函数中,javascript,d3.js,Javascript,D3.js,我想编辑dragmove函数以接受参数。 这是原始代码: var drag = d3.behavior.drag() .on("drag", dragmove) .on("dragend", function(d){console.log("dragend"); }); focus.selectAll("circle") .data([coordinatesForecastCurrentMonth]) .
var drag = d3.behavior.drag()
.on("drag", dragmove)
.on("dragend", function(d){console.log("dragend"); });
focus.selectAll("circle")
.data([coordinatesForecastCurrentMonth])
.enter()
.append("circle")
.attr("cx", function(d) {
return x(d[0]);})
.attr("cy", function(d) {
return y(d[1]);})
.attr("r", function(d) {
return 5;})
.attr("clip-path", "url(#clip)")
.call(drag);
function dragmove(d) {
d3.select(this)
.attr("cy", d3.event.y);}
当我将其更改为以下内容时:
.on("drag", dragmove(1,2))
function dragmove(arg1,arg2) {
d3.select(this)
.attr("cy", d3.event.y);
console.log(arg1);
console.log(arg2);}
我得到这个错误:
我不明白为什么找不到事件对象,有人能帮忙吗?您正在更改中调用函数,而不是传递对它的引用。也就是说,您在执行代码时调用它,而不是拖动。因此,没有
d3.event
。将其作为这样的函数引用
.on("drag", function() { dragmove(1,2); });
代码的其余部分可以保持不变。非常感谢!我必须阅读更多关于调用函数和传递函数引用之间区别的内容。