2013-01-23 201 views
1

我想用jquery ui來模擬拖放,當拖放時不會從原始列表中刪除項目,怎麼可能?在這種情況下,我想保留在圖庫中的項目,但克隆到垃圾桶。Jquery droppable不刪除元素

Jsbin例如http://jsbin.com/igevut/1/edit

$trash.droppable({ 
     accept: "#gallery > li", 
     activeClass: "ui-state-highlight", 
     drop: function(event, ui) { 
     deleteImage(ui.draggable); 
     } 
    }); 

function deleteImage($item) { 
     $item.fadeOut(function() { 
     var $list = $("ul", $trash).length ? 
      $("ul", $trash) : 
      $("<ul class='gallery ui-helper-reset'/>").appendTo($trash); 

     $item.find("a.ui-icon-trash").remove(); 
     $item.append(recycle_icon).appendTo($list).fadeIn(function() { 
      $item 
      .animate({ width: "48px" }) 
      .find("img") 
       .animate({ height: "36px" }); 
     }); 
     }); 
    } 

回答

2

只需在deleteImage功能開始添加$item = $item.clone()

0

您可以使用一種方法來返回一個助手,該助手將充當可拖動對象的克隆對象。

$("li", $gallery).draggable({ 
     cancel: "a.ui-icon", // clicking an icon won't initiate dragging 
     revert: "invalid", // when not dropped, the item will revert back to its initial position 
     containment: "document", 
     helper: getHelper, 
     cursor: "move" 
    }); 

function getHelper(event){ 

    // return html for the helper 
} 

參見鏈路http://shyalika.com/create_drag_and_drop_example爲例