为什么在d3.behavior.drag()中没有定义`d`

chr*_*ina 5 javascript svg d3.js

我正在尝试使圈子可拖动。

var drag = d3.behavior.drag();
  drag.on("drag", function(d,i) {
      console.log(d);
      d.x += d3.event.dx;
      d.y += d3.event.dy;
      //This will change the center coordinates by the delta
      d3.select(this).attr("x", d.x).attr("y", d.y);
      //This should change the upper left x and y values by the delta
      d3.select(this).attr("transform", function(d,i){
          return "translate(" + [ x,y ] + ")"
      })
  })
Run Code Online (Sandbox Code Playgroud)

这是小提琴

它在右红色圆圈上的每一步都会引发错误,但是,为什么d在第3、4和5行中未定义呢?

Lar*_*off 5

工作提琴:http : //jsfiddle.net/6a6da/33/

d,i参数通常会提到绑定的数据,但你不绑定任何数据。就您而言,仅处理事件就足够了。

drag.on("drag", function() {
  d3.select(this).attr("cx", +d3.select(this).attr("cx") + d3.event.dx);
  d3.select(this).attr("cy", +d3.select(this).attr("cy") + d3.event.dy);
})l
Run Code Online (Sandbox Code Playgroud)