jQuery拖放 - 如何获取被拖动的元素

2022-01-11 00:00:00 drag-and-drop jquery javascript

我正在使用 jQuery 库来实现拖放.

I am using the jQuery library to implement drag and drop.

如何在拖放时找到正在拖动的元素?

How do I get at the element that is being dragged when it is dropped?

我想在 div 中获取图像的 id.拖动以下元素:

I want to get the id of the image inside the div. The following element is dragged:

<div class="block">
    <asp:Image ID="Image9" AlternateText="10/12/2008 - Retina" Width=81 Height=84 ImageUrl="~/uploads/ImageModifier/retina.jpg" runat=server />
</div>

我有他们示例中的标准删除函数:

I have the standard dropped function from their example:

$(".drop").droppable({
                 accept: ".block",
                 activeClass: 'droppable-active',
                 hoverClass: 'droppable-hover',
                 drop: function(ev, ui) { }
});

我尝试了各种 ui.id 等似乎不起作用.

I have tried various ui.id etc. which doesn't seem to work.

推荐答案

不是ui.draggable吗?

Is it not the ui.draggable?

如果你去这里(在 Firefox 中并假设你有 firebug)并查看 firebug 控制台,你会看到我正在做一个 ui.draggable 对象的 console.dir,它是被拖动的 div

If you go here (in Firefox and assuming you have firebug) and look in the firebug console youll see I am doing a console.dir of the ui.draggable object which is the div being dragged

http://jsbin.com/ixizi

因此你在drop函数中需要的代码是

Therefore the code you need in the drop function is

       drop: function(ev, ui) {
                 //to get the id
                 //ui.draggable.attr('id') or ui.draggable.get(0).id or ui.draggable[0].id
                 console.dir(ui.draggable)  
       }

相关文章