javascript - jquery 拖放,不允许放置在克隆放置容器上

标签 javascript jquery

尝试将项目拖放到克隆 div,但它不起作用。看起来 jquery 拖放不允许放在克隆项目上。

在我单击添加更多放置区后,在第一个上进行拖放操作,并尝试将项目放置到新的放置区。这是不允许的。

这是我的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" );
    }
});

任何帮助将不胜感激!

最佳答案

怎么样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");
        }
    });
}

关于javascript - jquery 拖放,不允许放置在克隆放置容器上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27241210/

相关文章:

jquery - 从 div 追加到 div 并返回

javascript - 当我的输入为空时,为什么 $(…).val() == ""false ?

javascript - 使用 javascript/jquery 数组生成选择菜单

Jquery bootstrap select2 插件问题与验证插件

javascript - 字符串的粗体部分/向变量添加样式

javascript - jQuery 进度条标签显示 "false"

javascript - 在文本/链接内容旁边放置图像

javascript - 如何在 if 语句中使用 css 类滚动页面

javascript - 无法将变量的值从 true 更改为 false

javascript - React Native - 如何获取数组映射组件的 key