使用jQuery在拖放事件期间提取元素值

时间:2015-11-17 20:26:24

标签: javascript jquery html

我有以下代码,它会将我附加了droppable事件的元素的ID打印到:

$('#myDiv').droppable({
    over: function(event, ui) {
        console.log('ID of the item you are hovering over ' + this.id);
    }
});

然而,我宁愿做的是将实际项目的值打印在<div>上。

<span class="myClass">This is the text that should be printed</span>

因此,在这种情况下,我每次使用spanmyClass拖过myDiv范围内的实际文本时,都会{。}}。

我可以用$('.myClass').text()阅读文字,但我不知道如何实现我上面描述的内容。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您应该能够通过event.target获取活动的目标。所以要阅读文本,代码应该是:

$('#myDiv').droppable({
    over: function(event, ui) {
        console.log($(event.target).text());
    }
});
相关问题