jquery 可排序/可拖动双事件触发
我有一些拖&删除按原样工作正常的代码。只是有一点疑问。我注意到,如果我在放置函数中添加一个警报以进行调试(例如alert(draggedItem.text());),当我将某些东西放入可拖动区域时,它会触发警报两次。我在另一篇文章中读到使用 droppable &可排序在一起会导致这种奇怪的双重事件发生。但我需要使用 droppable 事件来获取拖动的项目对象 (ui.draggable) - 这样我就可以在放置它时对其进行操作。如果有任何其他方式获取可拖动对象,请告诉我:)另外,如果您对为什么会发生这种情况有解释,那会很有趣...
$(".field > li").draggable({
helper:'clone',
opacity: 0.4,
connectToSortable:'.dragrow'
});
$(".dragrow").droppable({
drop: function(e, ui) {
draggedItem = ui.draggable;
//alert(draggedItem.text());
}
}).sortable({ //code here to do stuff with 'draggedItem'
我还有另一个与此相关的查询,但因为我的代码相当太大了,我无法在这里发布完整的内容。所以我理解如果你无法提供帮助——如果你突然想到什么那就真的很酷了。基本上我有一个“块”列表,我可以将其拖入多行。可以使用切换事件隐藏各个行。如果我有 3 行,我可以将块拖到其中的任何一行中。如果我隐藏第一行,我现在无法拖动到其他两行。不过我仍然可以对它们进行排序。一旦我开始对它们进行排序,我就可以再次拖入它们。诡异的...
I have some drag & drop code which works fine as it is. Just have a little query. I've noticed that if I add an alert within the drop function for debugging purposes (eg. alert(draggedItem.text());) it fires the alert twice when I drop something into the draggable area. I've read in another post that using droppable & sortable together causes this weird double event to happen. But I need to use the droppable event to get the dragged item object (ui.draggable) - this is so I can manipulate it when I drop it. If there is any other way of getting the draggable object, please tell me :) Also if you have an explanation to why this happens, that would be interesting...
$(".field > li").draggable({
helper:'clone',
opacity: 0.4,
connectToSortable:'.dragrow'
});
$(".dragrow").droppable({
drop: function(e, ui) {
draggedItem = ui.draggable;
//alert(draggedItem.text());
}
}).sortable({ //code here to do stuff with 'draggedItem'
I also have another query related to this, but as my code is quite large, I'm unable to post the full thing here. So I understand if you are unable to help - just if something springs to mind that would be really cool. Basically I have a list of 'blocks' which I can drag into multiple rows. Individual rows can be hidden with the toggle event. If I have 3 rows, I can drag blocks into any of them. If I then hide the first one, I am now unable to drag into the other two rows. I can still sort them though. And once I start sorting them, I am then able to drag into them again. Weird...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查演示 http://jsfiddle.net/yeyene/FUyTe/1/
正如您提到的,是的,使用 droppable &可排序在一起会导致这种奇怪的双重事件发生。但是,如果您仍然想使用
draggedItem对象
,您仍然可以将其与可排序接收选项一起使用。此外,您还可以使用 ui.item 来获取当前拖动的元素。
尝试对每个警报发表评论,然后看看,现在它只触发一次。
这是修改删除元素的 html 的一种方法。
演示 http://jsfiddle.net/yeyene/7fEQs/8/
Check the DEMO http://jsfiddle.net/yeyene/FUyTe/1/
As you mentioned, yes, using droppable & sortable together causes this weird double event to happen. But, if you still want to use
draggedItem object
, you still can use this with sortable receive option.And also, you can use
ui.item
to get the current dragged element.Try to comment on and off each alert and see, now it fires only once.
Here is one way to modify dropped element's html.
DEMO http://jsfiddle.net/yeyene/7fEQs/8/
您是否检查过您的网站上没有两次使用 jquery 脚本标记?
Have you checked that you don't have your jquery script tag twice on your site?