jQuery droppable Draggable 删除 DnD 多个

发布于 2024-12-29 14:02:32 字数 740 浏览 2 评论 0原文

我之前发布了一个 DnD 问题,现已解决,但在测试过程中我发现其他代码中存在错误:“删除”操作。
我确信这是完全合乎逻辑的,而且我缺少对它的逻辑解释,但我在互联网上(还)或在几乎没有接触过 DnD 的书中找不到任何答案。

问题是,当一个 droppable 已经被多个 (>1) droppable 瞄准,当对其执行 REMOVE 操作时,将导致返回所有之前丢弃的内容。

亲自查看:http://jsfiddle.net/BCnyU/

请按照以下步骤操作:

1) 在“放下 3”上“拖动 2”
2) 在“拖放 4”上“拖 1”
3) 在“放下 5”上“拖动 4”
    没什么特别的,只是为了让他们别挡我们的路。
现在执行此操作:
4) 在“放下 1”上“拖动 3”
5) 单击“拖动3”的“撤消”
6) 在“放下 1”上“拖动 5”
7) 在“放下 2”上“拖动 3”

发生的情况是,我们在“drop 1”上发生了两 (2) 个放置事件。
当您现在单击“drag 5”的“撤消”时,您可能会期望仅返回“drag 5”。
但是不不...“拖5”和“拖3”将会出现!

谁能给我解释一下,并给出解决方案?

I posted earlier an issue with DnD that is solved now but during testing I saw that there was an error in the other code: the "remove" action.
I am sure it is totally logical, and that I am missing the logical explanation for it but I can't find any answers on the internet (yet) nor in the books where DnD is hardly touched.

The issue is that when a droppable has been targetted with more than one (>1) droppable, It will result in a return of ALL earlier dropped when doing the REMOVE on it.

See for your self : http://jsfiddle.net/BCnyU/

Follow these steps:

1) "drag 2" on "drop 3"
2) "drag 1" on "drop 4"
3) "drag 4" on "drop 5"
    Nothing special, just to get them out of our way.
Now do this:
4) "drag 3" on "drop 1"
5) click "undo" of the "drag 3"
6) "drag 5" on "drop 1"
7) "drag 3" on "drop 2"

What happend is that we have had TWO (2) drop events on the "drop 1".
When you click now on the "undo" of "drag 5", you might expect that "drag 5" only will return.
But NO NO... "drag 5" and "drag 3" will appear !!!

Who can explain me that, and give also a solution ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

堇年纸鸢 2025-01-05 14:02:32

每次将元素拖到放置区域时,都会将单击处理程序附加到该放置区域。这些点击处理程序永远不会被删除,因此除了添加的新点击处理程序外,它们还保留在该区域中。添加和删​​除元素越多,绑定的点击处理程序就越多。

在不更改代码的情况下解决此问题的最简单方法是将 click 更改为 oneone 允许您附加一个仅执行一次的处理程序。

http://jsfiddle.net/BCnyU/101/

      .one("click",function(){
        $(ui.draggable).show();
        $(this).css("color","black");
        $(this).text(oldvalue);
        $(this).droppable({disabled:false});
        $(this).removeClass('OK NOK');
        var numItems = $('.OK').length;
        $('.counter').html('Correct: '+numItems +'/5'); 
      });

另一种选择是将点击处理程序添加到“UNDO”锚标记,而不是整个放置区域。然后,当“UNDO”锚点被删除时,处理程序也将被删除。

Every time an element is dragged onto a drop area, you are attaching a click handler to that drop area. These click handlers are never removed so they remain on the area in addition to the new one being added. The more you add and remove elements, the more click handlers are bound.

The easiest way to fix this without changing your code would be to change click to one. one allows you to attach a handler which will only execute once.

http://jsfiddle.net/BCnyU/101/

      .one("click",function(){
        $(ui.draggable).show();
        $(this).css("color","black");
        $(this).text(oldvalue);
        $(this).droppable({disabled:false});
        $(this).removeClass('OK NOK');
        var numItems = $('.OK').length;
        $('.counter').html('Correct: '+numItems +'/5'); 
      });

Another option would be to add the click handler to the "UNDO" anchor tag, rather than the entire drop area. Then when the "UNDO" anchor is removed the handler would be removed as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文