使用jQuery-ui,我将可拖动元素拖放到可拖放元素上。然后,我将droppable元素变成可拖动元素。然后,如果我拖动新的可拖动元素,则希望该元素及其内容被删除(删除)。我可以使droppable元素可拖动;但是有两个问题:
在此eaxample中,“ 4WD”被拖动(克隆)到“活动”下方的可放置字段中。原始的“ 4WD”保持可拖动状态(根据需要)。克隆的“ 4WD”也可以拖动(不需要)。现在,可放置字段是可拖动的;但是,拖动时不会删除。
代码是:
function makeDraggable() {
$('.dragabbleItem').draggable({
stack: ".dragabbleItem",
cursor: 'pointer',
helper: 'clone',
});
}
function makeDraggableRemove() {
$('.dragabbleRemove').draggable({
cursor: 'pointer',
drag: function( event, ui ) {
$(ui.draggable).remove();
}
});
}
function makeDroppable() {
$('.droppableItem').droppable({
hoverClass: 'hovered',
drop: function( event, ui ) {
var droppable = $(this);
var draggable = ui.draggable;
alert( 'The item with ID "' + draggable.attr('id') + '" was dropped onto me!' );
// Move draggable into droppable
var drag = $('.droppableItem').has(ui.draggable).length ? draggable : draggable.clone().draggable({
revert: "invalid",
stack: ".dragabbleItem",
helper: 'clone'
});
drag.appendTo(droppable);
droppable.css({top: '5px', left: '5px', background: '#B0C4DE'});
droppable.droppable('disable');
droppable.addClass("dragabbleRemove");
makeDraggableRemove();
}
});
}
我终于找到答案,是要更改“ $(ui.draggable).remove();”。到“ $(this).remove();”
function makeDraggableRemove() {
$('.dragabbleRemove').draggable({
cursor: 'pointer',
drag: function( event, ui ) {
$(this).remove();
}
});
}
尽管这会在右后方留下一条粗的灰色垂直线。我也希望将其删除:-)