jQuery 拖放:克隆的可拖动移动原始内容
我在使用拖放时遇到问题放入 jQuery。在我的代码中有一个可拖动的 div。当我将其移动到某个可放置的表格单元格时,div 的克隆将附加到该表格单元格。但是当我拖动克隆时,原始 div 会被移动。这是 drop 时调用的函数:
function(event, ui)
{
var draggable = ui.draggable.clone(true); // cloning including attrs and children
draggable.draggable(); // this is something I tried with no effect
$(this).empty(); // empty the droppable cell
$(this).append(draggable); // append the div to the cell
}
我无法通过谷歌搜索找到明确的答案。我不知道这是否与此有关,但 div 有一个相对位置(并且需要保持这种状态)。
如何确保克隆可以像原始版本一样被拖动?
I've got a problem with Drag & Drop in jQuery. In my code there is a draggable div. When I move it to a certain droppable table cell, a clone of the div is appended to the table cell. But when I drag the clone, the original div is moved instead. This here is the function called on drop:
function(event, ui)
{
var draggable = ui.draggable.clone(true); // cloning including attrs and children
draggable.draggable(); // this is something I tried with no effect
$(this).empty(); // empty the droppable cell
$(this).append(draggable); // append the div to the cell
}
I couldn't find a clear answer by Googling. I don't know if it's got anything to do with this, but the div has a relative position (and needs to stay that way).
How do I make sure the clone can be dragged just like its original?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这就是我要做的,这应该可以解决问题。
我不知道你是否这样做了,但不要在可拖动对象上使用 id-s,因为它们也会被克隆。另外,您必须先附加该元素,然后再对其应用可拖动元素。
This is how I would do it, this should do the trick.
I don't know if you did, but do not use id-s on the draggable, cause those are going to be cloned too. Also, you have to append the element before applying the draggable on it.
更改您的
draggable
初始化代码以包含helper: "clone"
否则,您将拖动原始项目,该项目将留在完成拖动操作的位置。
Change your
draggable
init code to includehelper: "clone"
Otherwise you are dragging the original item which just gets left where you finished up the drag operation.
您是否尝试过将 false 传递给 .clone() ?该 bool 代表 withDataAndEvents,因此您将继承附加到旧对象的所有相同事件。
编辑:如果您出于其他原因需要 withDataAndEvents,您可以尝试在进行克隆之前在原始文件上使用draggable(“destroy”),然后在克隆之后再次使其可拖动?不是最优雅的,但它有效: http://jsfiddle.net/GheD5/
have you tried passing false into .clone()? That bool stands for withDataAndEvents, so you're inheriting all the same events that are also attached to the old object.
Edit: if you need withDataAndEvents for other reasons, you can try using draggable("destroy") on the original right before you do the clone, then make it draggable again after? Not the most elegant but it works: http://jsfiddle.net/GheD5/
您看过帮助选项吗?
Have you looked at the Helper Option?
你能展示完整的代码吗?我现在只是猜测,但是使用 'delegate()' http://api.jquery.com/delegate / 可能是您解决方案的关键。
Can you show the complete code? I'm just guessing now, but the using 'delegate()' http://api.jquery.com/delegate/ could be the key to your solution.