拖动jQuery中已删除元素的克隆

Drag the clone of dropped elements in jQuery

本文关键字:元素 删除 jQuery 拖动      更新时间:2023-09-26

我曾试图拖动被拖动元素的克隆,但当我拖动该元素时,它会在页面中创建另一个元素。这是我的密码。。

jQuery(function() {
    jQuery(".component").draggable({
        //  use a helper-clone that is append to 'body' so is not 'contained' by a pane
        helper:  function () { 
            return jQuery(this).clone().appendTo('body').css({'zIndex':5}); 
        },
        cursor: 'move'
    });
    jQuery('.ui-layout-center').droppable({
        activeClass: 'ui-state-hover',
        accept:  '.component',
        drop:  function(event, ui) {
            jQuery(this).append(jQuery(ui.draggable).clone().draggable()); 
        }
    });
});

是否有丢失的命令使每次拖动都成为一个新的克隆?

您应该有一个类来指示元素已经被删除,并且您可以移动它,但不应该克隆它。你可以看看这把小提琴:http://jsfiddle.net/scaillerie/njYqA/。在drop的处理程序中,我刚刚用以下代码替换:

if (!ui.draggable.hasClass("dropped"))
    jQuery(this).append(jQuery(ui.draggable).clone().addClass("dropped").draggable());

jQuery(this).append(jQuery(ui.draggable).clone().draggable());你忘了.clone() 了吗