获取被拖动的SVG元素的ID

时间:2019-01-05 17:19:39

标签: javascript svg.js

我正在使用 svg.js dragg.js 来移动一系列动态创建的对象(矩形和圆形等)。 它们全都在一个名为.SingleOrDefault()的组中,我可以确定要拖动的项目的位置,但是在尝试检索

时迷失了方向

g nodes

我需要这个来识别哪个SVG对象被移动了吗?

id="dyanmicidIcreated"

2 个答案:

答案 0 :(得分:2)

您将处理程序传递给该函数,该函数与被触发的事件一起调用。 因此,有关事件的所有信息都在事件对象中(出于某种原因,您将其称为node)。

nodes.mouseup((event) => {
    // mouse coordinates
    console.log(event.clientX, event.clientY)

    // the node which was clicked
    console.log(event.target)

    // the svgjs object
    console.log(SVG.adopt(event.target))

    // the id
    console.log(SVG.adopt(event.target).id())
})

如果不使用箭头函数,则在svgjs对象的范围内调用该函数。因此,您只需使用this

nodes.mouseup(function (event) {
    // `this` is same as node
    console.log(this)

    // id
    console.log(this.id())
})

在svg.js v3.0中,您将使用SVG(event.target)而不是SVG.adopt。其他一切保持不变

答案 1 :(得分:0)

console.log(SVG.adopt(event.target.parentNode).id())

相关问题