jQuery拖放 - 如何获取被拖动的元素 [英] jQuery drag and drop - how to get at element being dragged

查看:35
本文介绍了jQuery拖放 - 如何获取被拖动的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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)  
       }

这篇关于jQuery拖放 - 如何获取被拖动的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆