jquery拖放,不允许删除克隆drop容器

时间:2014-12-02 03:07:50

标签: javascript jquery

尝试拖动项目并放弃克隆div,但它不起作用。它看起来像jquery drag n drop不允许放弃克隆项目。

在我点击添加更多dropzone之后,

拖动n drop work,然后尝试将项目拖放到新的dropzone。它不允许。

这是我的jsfiddle

$(document).on("click", ".add_dropzone", function(e){
$('<div class=\"dropzone\"></div>').insertAfter($('.dropzone').last());});

$( "ul li" ).each(function(){
    $(this).draggable({
        helper: "clone"
    });
});

$( ".dropzone" ).droppable({
    activeClass: "active",
    hoverClass: "ui-state-active",
            tolerance: "touch", 
    accept: ":not(.ui-sortable-helper)",
    drop: function( event, ui ) {

        var targetElem = $(this).attr("id");

        $( this ).addClass( "ui-state-highlight" );

        if($(ui.draggable).hasClass('draggable-source'))
            $( ui.draggable ).clone().appendTo( this ).removeClass('draggable-source');


        else

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

            $('.delete').on('click', function () {
            $(this).parent().parent().parent().parent('li').remove();
        });
        //alert($(this).text());
    }

}).sortable({
    items: "li:not(.placeholder)",
    sort: function() {
        $( this ).removeClass( "ui-state-default" );
    }
});

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

this

怎么样?

我已将初始化代码移动到函数

最终代码看起来像

$(document).on("click", ".add_dropzone", function (e) {
    $('<div class=\"dropzone\"></div>').insertAfter($('.dropzone').last());
    initDroppable();
});

$("ul li").each(function () {
    $(this).draggable({
        helper: "clone"
    });
});
initDroppable();

function initDroppable() {
    if (typeof window.dropElem != "undefined") window.dropElem.droppable('destroy');
    window.dropElem = $(".dropzone").droppable({
        activeClass: "active",
        hoverClass: "ui-state-active",
        tolerance: "touch",
        accept: ":not(.ui-sortable-helper)",
        drop: function (event, ui) {

            var targetElem = $(this).attr("id");

            $(this).addClass("ui-state-highlight");

            if ($(ui.draggable).hasClass('draggable-source')) $(ui.draggable).clone().appendTo(this).removeClass('draggable-source');


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

            $('.delete').on('click', function () {
                $(this).parent().parent().parent().parent('li').remove();
            });
            //alert($(this).text());
        }

    }).sortable({
        items: "li:not(.placeholder)",
        sort: function () {
            $(this).removeClass("ui-state-default");
        }
    });
}