如何在d3事件绑定时访问原始事件对象

How to access original event object when d3 event binding

本文关键字:事件 访问 原始 对象 定时 绑定 d3      更新时间:2023-09-26

在d3中,我已将节点上的mousedown事件绑定到我要执行的函数:

let node = gA.svg.selectAll('.node').data(gA.force.nodes(), gA.node_id)
let node_group = node.enter().append('g')
    .classed('node', true)
    .call(gA.drag)
node_group.append('circle')
    .classed('node-circle', true)
    // we may consider adding the position too. it will get updated on the 
    // next tick anyway, so we will only add it here if things look glitchy
    .attr('r', gA.node_radius) 
    .on(gA.circle_events)
    .on('mousedown', mousedown)
    .on('mouseup', mouseup)

只有最后一行才是真正相关的。还有我使用d3的事实。

但现在当我定义mouseup函数时。。。

function mouseup(datum, something1, something2) {
    alert("But where is the Event object?  :(  :'(  ")
}

基准面是绑定到svg"圆"的基准面。something1似乎是鼠标相对于元素的x位置(我的最佳猜测)。2看起来是y的位置。

但是我如何访问活动?我需要访问Event.clientX.

这是有据可查的:

d3.事件

存储当前事件(如果有)。在使用on运算符进行事件侦听器回调期间,会注册此全局。在finally块中通知侦听器后,将重置当前事件。这允许侦听器函数具有与其他运算符函数相同的形式,传递当前数据d和索引i。