克隆后,div不再可以丢弃了

时间:2017-09-21 15:47:11

标签: jquery jquery-ui droppable

on this website我试图克隆并从左到右拖放对象。

要拖放我没有问题,但碰巧我克隆一个div后不再可以放弃了。

删除后,克隆的div会在我的可放置“区域”上自动对齐。

如果没有克隆它们,就不会发生这种情况。

$(function() {
  $("#dragrabble-one").draggable({
    helper: 'clone'
  });
  $("#dragrabble-two").draggable({
    helper: 'clone'
  });
  $("#dragrabble-three").draggable({
    helper: 'clone'
  });
  $("#dragrabble-four").draggable({
    helper: 'clone'
  });
  $("#dragrabble-five").draggable({
    helper: 'clone'
  });
  $("#dragrabble-six").draggable({
    helper: 'clone'
  });
  $("#dragrabble-seven").draggable({
    helper: 'clone'
  });
  $("#dragrabble-eight").draggable({
    helper: 'clone'
  });
  $("#dragrabble-one").draggable({
    revert: "invalid"
  });
  $("#dragrabble-two").draggable({
    revert: "invalid"
  });
  $("#dragrabble-three").draggable({
    revert: "invalid"
  });
  $("#dragrabble-four").draggable({
    revert: "invalid"
  });
  $("#dragrabble-five").draggable({
    revert: "invalid"
  });
  $("#dragrabble-six").draggable({
    revert: "invalid"
  });
  $("#dragrabble-seven").draggable({
    revert: "invalid"
  });
  $("#dragrabble-eight").draggable({
    revert: "invalid"
  });
  $("#droppable-box").droppable({
    drop: function (event, ui) {
	ui.helper.clone().appendTo('#droppable-box');
}
  });
});
.box
{
  height: 30px;
  width: 50px;
  border: 1px solid black;
  background: green;
}

#droppable-box
{
  height: 400px;
  width: 200px;
  border: 1px solid black;
  background: lightgray;
}

.container
{
  border: 1px solid black;
  float: left;
  margin: 0 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet"/>

<span>Drag green boxes onto gray dropzone</span>

<div class="container">
  <div class="box" id="dragrabble-one"></div><br />
  <div class="box" id="dragrabble-two"></div><br />
  <div class="box" id="dragrabble-three"></div><br />
  <div class="box" id="dragrabble-four"></div><br />
  <div class="box" id="dragrabble-five"></div><br />
  <div class="box" id="dragrabble-six"></div><br />
  <div class="box" id="dragrabble-seven"></div><br />
  <div class="box" id="dragrabble-eight"></div><br />
</div>

<div class="container">
  <div id="droppable-box">Drop Zone</div>
</div>

编辑:我试过的也是这个

$("#droppable-box").droppable({
  drop: function (event, ui) {
    var clone = ui.helper.clone();
    clone.draggable();
    clone.appendTo('#droppable-box');
  }
});

没有任何结果(在我的网站上)。

1 个答案:

答案 0 :(得分:1)

那是因为克隆需要用draggable实例化。

试试这个:

$("#droppable-box").droppable({
  drop: function (event, ui) {
    var clone = ui.helper.clone();
    clone.draggable();
    clone.appendTo('#droppable-box');
  }
});