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

javascript - In jQuery, what's the proper way to "move" an element from its parent to another element?

programmeradmin8浏览0评论

Using jQuery 1.4 and jQueryUI 1.8

Specifically, I'm using draggables/droppables, and when dropped, I would like to move the draggable (it's children, events, etc) from belonging to its parent element to be appended/added as a child of the drop target.

I know that in the droppable drop option, I can supply the following callback:

function(event, ui) {
    // stuff
}

where $(this).target will be the drop target, and ui.draggable will be the child element I would like to move - but I'm not sure the proper way to actually perform the move, preserving events, etc.

Using jQuery 1.4 and jQueryUI 1.8

Specifically, I'm using draggables/droppables, and when dropped, I would like to move the draggable (it's children, events, etc) from belonging to its parent element to be appended/added as a child of the drop target.

I know that in the droppable drop option, I can supply the following callback:

function(event, ui) {
    // stuff
}

where $(this).target will be the drop target, and ui.draggable will be the child element I would like to move - but I'm not sure the proper way to actually perform the move, preserving events, etc.

Share Improve this question asked Jan 25, 2011 at 16:55 anonymous cowardanonymous coward 12.8k13 gold badges58 silver badges99 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 14

append() will remove the element and place it where you want.

$(this).target.append(ui.draggable);

// or, if $(this).target is not a jQuery object

var target = $(this).target;
$(target).append(ui.draggable);

Just use .append(), .appendTo(), .prepend() or .prependTo(). The detaching part is implicit. (I tested this by re-parenting entries in the jQuery Manipulation docs to each other.)

ui.draggable.appendTo($(this).target);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论