jquery 可删除接受
谁能告诉我如何在接受条件下编写一个函数,然后它如何找出接受什么和不接受什么。 例如,我想在接受条件下接受 div a
和 div b
。我如何通过函数来写I?
Can anyone tell me how I can write a function in accept condition and then how does it finds out that what to accept and what not to accept.
For example, I want to accept div a
and div b
in accept condition. How can I write I through a function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您希望 droppable 接受一系列元素,您可以执行以下操作:
This droppable 将接受类为“foo”的元素或 id 为“bar”的元素
If you want the droppable to accept a selection of elements you can do something like this:
This droppable will accept elements with the class "foo" or an element with the id "bar"
如果我正确理解您的问题,您希望根据自定义代码有条件地接受删除的元素。 Aberon 的答案是典型的情况:您只想允许根据其类别删除某些可拖动选项,而所有其他选项都将恢复。如果这回答了你的问题,那就好了。
但是,有时会根据比类匹配更复杂的情况有条件地发生恢复动画。在我自己的项目中,我使用拖放将用户添加到组中。如果删除的用户已经在组中,我希望用户帮助器元素恢复回来。否则,我将继续执行 AJAX 操作来插入它们。这不能替代后端检查,但它是很好的视觉反馈。
我在其他地方寻找过这个问题的简单答案。 JQuery 维护者请注意:对我来说,最直接的方法是在 drop 函数中向事件对象添加一些属性,如下所示:
遗憾的是,这不起作用。不过,“有效”的方法如下:将可拖动元素设置为始终恢复,然后在满足条件时隐藏助手。您可以通过查找页面上唯一具有“.ui-draggable-dragging”类的元素来获取对帮助程序的引用。下面是一个示例,将“isDropOK()”替换为您自己的逻辑:
因此,回顾一下,除非您介入 drop 事件并手动隐藏助手,否则每个元素都将始终恢复。恢复动画仍然会发生,但您的用户不会看到它。虽然有点麻烦,但最终结果似乎没问题。
If I understand your question correctly, you want to conditionally accept the dropped element based on custom code. Aberon's answer is the typical case: you want to allow only certain draggable options to be dropped based on their class, and all others will revert. If that answers your question, then fine.
However, there is a case for having the revert animation happen conditionally based on something more complex than a class match. In my own project, I am using drag-drop to add users to a group. If the dropped user is already in the group, I want the user helper element to revert back. Otherwise, I go ahead with an AJAX action to insert them. This is no substitute for back-end checking, but it's nice visual feedback.
I have looked elsewhere for a simple answer to this. Note to JQuery maintainers: to me, the most straightforward way would be to add some property to the event object in the drop function, like this:
Sadly that doesn't work. Here's what does "work", though: set the draggable element to always revert, and then hide the helper if the condition is met. You can get a reference to the helper by looking for the only element on the page that has the class ".ui-draggable-dragging". Here's an example, replace "isDropOK()" with your own logic:
So to recap, every element will always revert unless you step in on the drop event and manually hide the helper. The revert animation will still happen, but your users won't see it. It's a little hack, but the end result seems to work all right.
根据有关选择器的 Jquery 文档。
因此,
在他们的示例中,如果它具有“特殊”类,则只会表现得好像有东西掉在上面一样。看起来它可以接受任何 Jquery 选择器。
According to the Jquery documentation on Selectors.
Thus,
in their example will only act as if something has been dropped on it if it has the class 'special'. It looks like it can accept any Jquery selector.
除了 Alex 答案(这是我在这个问题上找到的最佳答案)之外,我想补充一点,如果您想检查
droppable
和draggable
之间的匹配属性,您可以使用关键字this
访问accept
中的 droppable功能。这是我最近使用的一个小例子:因此,仅当
id_group
(记住,只是一个示例)与id_group
匹配时,才接受draggable
droppable
的 >,否则draggable
将被恢复。我认为这可能是一个非常常见的用例,也许这会对某人有所帮助。In addition to Alex answer which is the best answer I found on this question, I like to add, that if you want to check for matching attributes between
droppable
anddraggable
, you can use the keywordthis
to access the droppable within youraccept
function. Here is a small example I recently used:So the
draggable
is only accepted, when it'sid_group
(remember, just an example) matches with theid_group
of thedroppable
, otherwise thedraggable
would be reverted. I think this could be a very common use-case, maybe this will help someone.这是我使用的解决方案:
This is the solution that I use:
我已经找到了一个基于以下条件的解决方案:不应将任何可拖动对象放置在已被另一个可拖动对象占用的可放置对象中:
I've figured out a solution that works based on the condition that no draggable should be placed in a droppable already occupied by another draggable:
您可以使用类似的函数,而不是使用类作为接受
如果符合您的条件则返回 true
Instead of using a class as accept, you can just use a function like
and return true if it matches your criteria
这是我的解决方案:
This is my solution: