最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - drag drop and change parent of a div in dom - Stack Overflow

programmeradmin0浏览0评论

In

If i drag a div to "snaptarget" div.
Will the dragged div be shown INSIDE it as its child element in the DOM?
I hit f12 on browser and saw the position cordinates changing as I drag but showing that the parent of that dragged,dropped div was unchanged.

I think same happens with droppable /

Is there a way to check and be sure?

I do want this feature as I want to drag one div to another and later know the parent of that div.

In http://jqueryui./draggable/#snap-to

If i drag a div to "snaptarget" div.
Will the dragged div be shown INSIDE it as its child element in the DOM?
I hit f12 on browser and saw the position cordinates changing as I drag but showing that the parent of that dragged,dropped div was unchanged.

I think same happens with droppable http://jqueryui./droppable/

Is there a way to check and be sure?

I do want this feature as I want to drag one div to another and later know the parent of that div.

Share Improve this question edited Jul 28, 2013 at 2:14 Masood Ahmad asked Jul 28, 2013 at 2:06 Masood AhmadMasood Ahmad 7414 gold badges15 silver badges38 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 4

I got it working with Asad's help

http://jsfiddle/4bmT9/1/

and found this wonderful thing made by some one else

http://jsfiddle/l33r0y/yrLbE/48/

JS:

$('.dragme').draggable();
$('#drop, #source').droppable({
    drop:function(e, ui) {
        $(e.target).append($(ui.draggable).detach().css({'top':'', 'left':''}));
    }
});

HTML:

<div id="source" class="box">
    <div class="dragme">bacon</div>

</div>
<div id="drop" class="box"></div>

CSS:

.box { height:100px;width:100px;border:1px solid #333; }
.dragme { width:100%; margin:5px; border:1px solid #333; }

Here is a demonstration: http://jsfiddle/4bmT9/1/

The dragged element is either duplicated or transplanted as a child of the drop target (depending on what you've selected for the helper option).

To state this more explicitly, there will be a DOM node under the droppable element that contains the draggable element.

EDIT:

It appears I was mistaken. This isn't default behavior, but you could do something like this:

$(".target").droppable({
    accept: '.draggable',
    drop: function(event, ui) {
        $(this).append(ui.draggable);
    }
});
$(".draggable").draggable();

Here is a demonstration: http://jsfiddle/VTHcG/

发布评论

评论列表(0)

  1. 暂无评论