jquery 拖出时从 droppable 中移除

Dev*_*man 2 jquery jquery-ui jquery-ui-draggable jquery-ui-droppable

我基于示例购物车演示实现了 jQuery 的可拖放功能。当您将其拖出可放置对象时,我希望能够将其<li>从可放置对象中删除。我认为这可能与droppable out 事件有关,但 ui 参数为空。有谁知道解决方案吗?

A. *_*lff 5

这是一个完整的工作解决方案,尚未完全测试,您仍然应该对其进行调试:{重新分配可拖动到已放置的元素以触发退出事件} {您也应该重新分配可放置!}

查看演示

可编辑演示

$(function () {
    $("#catalog").accordion();
    $("#catalog li").draggable({
        appendTo: "body",
        helper: "clone"
    });
    $("#cart ol").droppable({
        out: function (event, ui) {
            var self = ui;
            ui.helper.off('mouseup').on('mouseup', function () {
                $(this).remove();
                self.draggable.remove();
            });
        },
        activeClass: "ui-state-default",
        hoverClass: "ui-state-hover",
        accept: ":not(.ui-sortable-helper)",
        drop: function (event, ui) {
            if (ui.draggable.is('.dropped')) return false;
            $(this).find(".placeholder").remove();
            $("<li></li>").text(ui.draggable.text()).appendTo(this).draggable({
                appendTo: "body",
                helper: "clone"
            }).addClass('dropped');
        }
    }).sortable({
        items: "li:not(.placeholder)",
        sort: function () {
            // gets added unintentionally by droppable interacting with sortable
            // using connectWithSortable fixes this, but doesn't allow you to customize active/hoverClass options
            $(this).removeClass("ui-state-default");
        }
    });


});
Run Code Online (Sandbox Code Playgroud)