简体   繁体   English

防止多次添加可拖动对象jQuery

[英]prevent draggable objects are added several times jQuery

I can't figure out how to prevent that draggable objects are added several times in one drag event. 我无法弄清楚如何防止在一个拖动事件中多次添加可拖动对象。 I made a small example where you can produce the problem. 我举了一个小例子,您可以在其中产生问题。

https://jsfiddle.net/richiwarmen/afqu96v3/1/ https://jsfiddle.net/richiwarmen/afqu96v3/1/

$( ".draggableEl" ).droppable({
      accept: ".dropme",
      drop: function( event, ui ) {
        $(this).append(ui.draggable.clone().css("left","0px"));
      }});
$( ".draggableEl" ).draggable();

$( ".dropme" ).draggable({
     revert: 'invalid', 
     helper: "clone" ,
});

drag the purple block in the upper left of the green block, . 将紫色块拖到绿色块的左上方,。

You have multiple droppable sibling divs on top of each other. 彼此之间有多个可放置的同级div。 When you drop onto one of them, the ones below it will also activate. 当您落入其中之一时,它下面的那些也将激活。

If you made them nested you could use the greedy: true option. 如果将它们嵌套,则可以使用greedy: true选项。 But in this case since your divs are all siblings, you can't really do much about it. 但是在这种情况下,由于您的div都是兄弟姐妹,因此您实际上无法做很多事情。

Demo - https://jsfiddle.net/Patosai/afqu96v3/2/ 演示-https: //jsfiddle.net/Patosai/afqu96v3/2/

See here - Jquery droppable - Greedy not working as expected . 请参阅此处-jQuery droppable-贪婪无法按预期工作

I think you just want to remove the word clone : 我认为您只想删除clone一词:

$( ".draggableEl" ).droppable({
      accept: ".dropme",
      drop: function( event, ui ) {
        $(this).append(ui.draggable.css("left","0px"));
      }});
$( ".draggableEl" ).draggable();

$( ".dropme" ).draggable({
     revert: 'invalid',
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM