尝试在此处学习d3(v5(
我正在尝试将一个事件侦听器添加到我生成的voronoi的一组路径中。
我想使用d3鼠标事件,但无法访问触发该事件的元素。
这是代码:
svg
.append("g")
.attr("class","cells")
.selectAll("path")
.data(voronoi.polygons(vertices))
.enter().append("path")
.attr("d",(d)=>{return "M" + d.join("L") + "Z"})
.on("mousemove",()=>{console.log(this)})
console.log只给了我整个窗口,我也尝试过使用d3.mouse(这个(,我在一些v3,4的例子中看到了它,但它给了我这个错误
Uncaught TypeError: t.getBoundingClientRect is not a function
也尝试了d3.svg.mouse(this(Uncaught TypeError: d3.svg.mouse is not a function
我也尝试了d3.touch(this(,因为。。。为什么不呢?它只返回null
我想尝试访问鼠标所在的路径,并更改样式/属性。我知道我可以用css做,但我正在学习d3,所以我想知道如何做。
感谢大家
github代码链接:https://github.com/Sidchou/d3-exercise
尝试使用标准函数,因为箭头函数使用外部词法范围:
.on("mousemove", function(){ console.log(this) })