将 javascript 函数应用于可拖动副本
我想应用名为 copy_contenue 的函数来更改 div 父 id 在我拖动原件后创建的副本上,但我的脚本更改了原件而不是副本我也尝试了 ui.helper 来代替它,但没有任何反应
$('#model_1').draggable({
connectToSortable:'#global_div',
addClasses: false,
helper:'clone',
stop: function(e,ui) {
$(this).replaceWith(copie_contenue("model_1"));
}
})
I want to apply function called copie_contenue that change the div parent id
on the copy I created after I dragged my original,but my script change the original not the copy I also tried ui.helper in the place of this but nothing happen
$('#model_1').draggable({
connectToSortable:'#global_div',
addClasses: false,
helper:'clone',
stop: function(e,ui) {
$(this).replaceWith(copie_contenue("model_1"));
}
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要更改新插入的项目,您应该使用可排序的接收事件。然而,截至今天,jQuery UI (1.8.11) 中有一个已知的限制/缺失功能,因此您无法轻松地从接收事件访问克隆的项目。现有的 ui.item 引用原始元素,而不是副本。
作为解决方法,您可以在拖动开始时向原始项目添加一个特殊的类,您可以在接收事件(在将克隆插入到 DOM 后触发)上搜索可排序的项目。在拖动结束时,您必须确保无论发生什么,文档中的任何元素都不应具有特殊的类集。如果您要复制/克隆,这一点尤其重要,因为可排序的接收事件会在可拖动的停止事件之前触发(我们从原始可拖动的特殊类中删除)。
http://jsfiddle.net/3Gkrf/1/
HTML:
CSS:
javascript:
如果你想要每当拖动停止时手动创建另一个实例,这可以在可拖动的停止事件上实现。但是,我认为没有可靠的方法来检测它是否被丢弃在可排序的地方或其他地方。
您必须手动克隆对象,因为即使您设置助手来克隆它,只要拖动停止,助手就会被销毁。如果将其放在可排序对象上,则最终可能会得到两个新对象,因为可排序对象在接收时会自动克隆。
To change the newly inserted item, you should use the sortable's receive event. However, as of today, there is a known limitation/missing feature in jQuery UI (1.8.11) so that you can't easily get access to the cloned item from the receive event. The existing ui.item references the original element, not the copy.
As a workaround, you can add a special class to the original item when dragging starts for which you can search the sortable's items on the receive event (which fires after the clone has been inserted into the DOM). At the end of the drag you have to make sure that whatever happens, no elements in your document should have the special class set. It's especially important if you're copying/cloning, as the sortable's receive event fires BEFORE the draggable's stop event (where we remove the special class from our original draggable).
http://jsfiddle.net/3Gkrf/1/
HTML:
CSS:
javascript:
If you just want to create another instance manually whenever dragging stops, that's possible on the draggable's stop event. However, I don't think there is a reliable way in there to detect wether it was dropped on the sortable or somewhere else.
You have to manually clone your object, because even though you set the helper to clone it, the helper gets destroyed whenever dragging stops. If it WAS dropped on a sortable, you might end up with two new objects though, as the sortable automatically clones on receive.
@DarthJDG:实际上它有效,但我将我的可排序项包含在可拖动脚本中,谢谢
});
@DarthJDG :actually it worked but i included my sortable in the draggable script thanks
});