2

我正在尝试使用以下代码获取我单击的图像的 ID:

theImg.on('click', function() {
    alert($(this).attr('id')); //Should show 'IDofImg'
});

Konva 代码是这样的:

var theImg = new Konva.Image({
    image: imageObj,
    x: stage.getWidth() / 2 - 200 / 2,
    y: stage.getHeight() / 2 - 137 / 2,
    opacity: 0.8,
    shadowColor: 'black',
    shadowBlur: 5,
    id: 'IDofImg',
    shadowOffset: {
        x: 0,
        y: 0
    },
    startScale: 1,
    shadowOpacity: 0.6,
    draggable: true
});

如您所见,我在制作图像时有id: 'IDofImg',但似乎没有输出所需的 ID。

它当前在单击时输出:

function() {
   // setting
   if (arguments.length) {
       this[setter](arguments[0]);
       return this;
   }
   // getting
   else {
       return this[getter]();
   }
}

我错过了什么?

在这里提琴

4

1 回答 1

2

您应该使用this.id()它,因为它是 Konva Image 对象,而不是html/javascript对象。

另请参阅文档: http: //konvajs.github.io/api/Konva.Node.html#id

于 2015-10-05T15:02:37.917 回答