【问题标题】:Get hovered element d3 v4 [duplicate]获取悬停元素d3 v4 [重复]
【发布时间】:2019-05-05 04:34:55
【问题描述】:
svg.append('circle')
    .attr('class', 'points')
    .attr("cx", d => x(d.date))
    .attr("cy", d => y(d.close))
    .attr("r", 10)
    .attr("fill", "#364652")
    .on('mouseover', (d, i) => {
          console.log(this)
     })

在上面的示例中 - this 返回窗口,因此该函数内部的 d3.select(this) 不起作用,就像在 v3 版本示例中那样。

如何获取鼠标悬停的元素?

【问题讨论】:

    标签: javascript d3.js


    【解决方案1】:

    我刚刚注意到我正在使用箭头函数,所以它在创建时绑定了上下文。

    如果你想获取触发事件的元素,你应该使用正则函数表达式:

    svg.append('circle')
        .attr('class', 'points')
        .attr("cx", d => x(d.date))
        .attr("cy", d => y(d.close))
        .attr("r", 10)
        .attr("fill", "#364652")
        .on('mouseover', function (d, i) {
              console.log(this)
         })
    

    【讨论】:

    • 箭头函数绑定 this
    • @rioV8 rly?有链接吗?
    • 可能在关于 JavaScript 的文档中。搜索“JavaScript 箭头函数”
    • @rioV8,是的,我已经读过了,文档说它在声明的地方被绑定。
    猜你喜欢
    • 2016-12-09
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    • 2021-01-15
    • 1970-01-01
    • 2019-06-26
    相关资源
    最近更新 更多