jQuery UI如何添加点击拖动元素

时间:2018-05-07 17:06:58

标签: jquery jquery-ui drag-and-drop

我有一个div类型的元素, 我想将它拖放到带有克隆的droppable元素上,然后使其可单击,以便我可以打开相关属性。 HTML

<div id="drag_website" class="d product">
    <p>Website</p>
    </div>

    <div id="drag_phone" class="d product2">
    <p>Phone</p>
    </div>

    <div id="droppable_container" class="ui-draggable-helper">
      <p>Drop your content here!!</p>
    </div>

脚本

<script type="text/javascript">
  $( document ).ready(function() {

    $('.d').draggable({
     containment: "window",
     helper: "clone",


    });


    $( "#droppable_container" ).droppable({
    accept: ".product",

    drop: function( event, ui ) {
       $(ui.draggable).clone().appendTo($(this));
       console.log('element dropped');
    }    

    });

    // Getter
    var accept =$('#droppable_container').droppable("option", "classes.ui-droppable" );
    var str = JSON.stringify(accept);
    console.log('element  ::: ' + accept);


});
</script>

1 个答案:

答案 0 :(得分:1)

扩展我的评论,你可以看到这个例子。

$(function() {
  $('.drag').draggable({
    containment: "window",
    helper: "clone",
  });

  $("#droppable_container").droppable({
    accept: ".product",
    drop: function(event, ui) {
      $(ui.draggable).clone().appendTo($(this)).click(function() {
        console.log("Dropped item clicked.");
      });
      console.log('element dropped');
    }
  });

  // Getter
  var accept = $('#droppable_container').droppable("option", "classes.ui-droppable");
  var str = JSON.stringify(accept);
  console.log('element  ::: ' + accept);
});
.drag {
  border: 1px solid #ccc;
  border-radius: 6px;
  display: inline-block;
  padding: .2em .4em;
}

.drag p {
  display: inline-block;
  margin: 0;
}

.drop {
  border: 1px solid #ccc;
  border-radius: 6px;
  padding: .25em;
  margin: 20px 10px;
  min-height: 100px;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.12/css/all.css" integrity="sha384-G0fIWCsCzJIMAVNQPfjH08cyYaUtMwjJwqiRKxxE/rx96Uroj1BtIQ6MLJuheaO9" crossorigin="anonymous">
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div class="ui-widget">
  <div id="drag_website" class="drag product">
    <i class="fas fa-file-code fa-2x fa-fw"></i>
    <p>Website</p>
  </div>
  <div id="drag_phone" class="drag product2">
    <i class="fas fa-phone-square fa-2x fa-fw"></i>
    <p>Phone</p>
  </div>
  <div id="droppable_container" class="drop">
    <p>Drop your content here!!</p>
  </div>
</div>

如果您必须引用回调或元素本身,您可以考虑不链接它。

drop: function(e, ui){
  var $self = $(this);
  var $myItem = $(ui.draggable).clone();
  $myItem.appendTo($self).click(function(event){
    console.log("event ::: click, ",  $(this));
  });
}

这有助于确保没有不明确的this个对象。

<强>更新

在附加克隆元素之前,您可以计算并更新已拖放到drop中的当前产品。

drop: function(e, ui){
  var $self = $(this);
  var $myItem = $(ui.draggable).clone();
  var c = $(".product", $self).length + 1;
  $myItem.attr("id", $self.attr("id") + "-" + c);
  $myItem.appendTo($self).click(function(event){
    console.log("event ::: click, ",  $(this));
  });
}

这会将克隆项目的id更新为唯一。第一个新删除的项目将具有属性:id="drag_website-1"。由于length0