Jquery拖放和克隆

嗨,我需要实现这个..

我有一套可放置的物品(基本上我是在服装上放下设计)而且我正在放弃一个克隆..

如果我不喜欢被丢弃的对象(设计) – 我想通过做一些像隐藏的东西来删除它。

但我无法做到这一点。

请帮我..


这是代码

var clone; $(document).ready(function(){ $(".items").draggable({helper: 'clone',cursor: 'hand'}); $(".droparea").droppable({ accept: ".items", hoverClass: 'dropareahover', tolerance: 'pointer', drop: function(ev, ui) { var dropElemId = ui.draggable.attr("id"); var dropElem = ui.draggable.html(); clone = $(dropElem).clone(); // clone it and hold onto the jquery object clone.id="newId"; clone.css("position", "absolute"); clone.css("top", ui.absolutePosition.top); clone.css("left", ui.absolutePosition.left); clone.draggable({ containment: 'parent' ,cursor: 'crosshair'}); $(this).append(clone); alert("done dragging "); /lets assume I have a delete button when I click that clone should dissapear so that I can drop another design - but the following code has no effect //and the item is still visible , how to make it dissapear ? $('#newId').css("visibility","hidden"); } }); }); 

由于.clone()返回一个jQuery对象。 clone.id =“newId”在jQuery对象上设置属性而不是DOM元素。 由于DOM元素没有id属性。 $(’#newId’)。length应返回null。 在firebug控制台中测试

使用:

 clone.attr('id', 'newId') 

在克隆对象的DOM元素上设置ID。

您可以使用drop dragabble对象function:

 drop: function (event, ui) { $(ui.draggable).remove(); } 

 $(this).append(clone); 

在你的脚本中;