jQuery Draggable 仍然可以拖放到空 - 我该如何阻止它?
如果我有一个 jQuery Draggable 对象
$('#drag-me').draggable({
helper: 'clone',
connectToSortable: '#sortable'
});
和一个连接的可排序对象,
$('#sortable').sortable({
receive: performMagic
});
我无法阻止此可拖动对象放在空可排序对象上。
Sortable 本身有一个选项 dropOnEmpty,当设置为 false 时,可以防止您将可排序的内容删除到空的可排序中;但 Draggable 没有这样的选项。
我尝试了这个:
$('#sortable').sortable({
start: function(e, ui) {
if (ui.item[0].nodeName == 'IMG') {
$(this).sortable('cancel')
}
}
});
在我的例子中,#drag-me 元素是图像,但可排序元素不是。 PerformMagic 函数成功地将图像转换为可排序包含的 TBODY 元素。但结果并不一致;有时它会成功取消操作,但有时它会在其他地方中断。
问题是,我走在正确的道路上吗?这是防止 Draggable 被拖放到空链接 Sortable 上的唯一方法吗?有没有办法可以劫持现有的可排序到可排序行为?
TIA 铝
If I have a jQuery Draggable object
$('#drag-me').draggable({
helper: 'clone',
connectToSortable: '#sortable'
});
and a connected sortable
$('#sortable').sortable({
receive: performMagic
});
I can't prevent this draggable from dropping on the empty sortable.
Sortable itself has an option dropOnEmpty, which, when set to false, prevents you dropping things from that sortable onto an empty sortable; but Draggable doesn't have such an option.
I tried this:
$('#sortable').sortable({
start: function(e, ui) {
if (ui.item[0].nodeName == 'IMG') {
$(this).sortable('cancel')
}
}
});
In my case the #drag-me element is an image but the Sortable elements are not. The performMagic function is successfully converting the image into the TBODY element that the sortable comprises. But results were inconsistent; sometimes it would successfully cancel the operation but other times it would break in other places.
The question is, am I on the right track? Is this the only way of preventing a Draggable from being dropped on an empty linked Sortable? Is there a way I can hijack the existing Sortable-to-Sortable behaviour?
TIA
Al
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你能在
receive
事件中检查sortable是否为空吗?像这样的东西:Can you check whether the sortable is empty in the
receive
event? Something like: