d3.mouse()需要什么参数

Aks*_*hot 3 javascript d3.js

我试图获取相对于group元素的鼠标坐标。这是代码:

var backRect = chart.append("g").attr("class", "rect");
var g = backRect.append('rect')
        .style('stroke', 'none')
        .style('fill', '#FFF')
        .style('fill-opacity', 0)
        .attr({
            width: width,
            height: height,
            'pointer-events': 'none',
            'class': 'backRect'
        });

// the code below is inside another container's event; but I want mouse coordinates relative to the above rect, hence can't use d3.mouse(this)
// get mouse pointer location
        var coordinates = [0, 0];
        coordinates = d3.mouse(backRect); // d3.select(".rect") does not work either
Run Code Online (Sandbox Code Playgroud)

但出现以下错误:

d3.min.js:1 Uncaught TypeError: n.getBoundingClientRect is not a function

根据d3 鼠标, docs d3.mouse()会使用一个可以是svgg元素的容器。

我应该传递给什么参数d3.mouse()?我尝试了d3.select(".rect")哪一种也不起作用。

Aks*_*hot 5

使用d3.mouse(backRect.node())做到了。