jquery 克隆似乎不保留可拖动/可放置事件
我有一排可以将项目拖入其中并对它们进行排序。这一切都正常。我什至对每个项目都有一个删除事件,因此可以将其从行中删除。
我想要一个可以克隆行的选项。我通过使用下面的克隆函数来完成此操作:
clonedrow = $("#row1").clone(true);
clonedid = "row"+nextRowNumber; //nextRowNumber is a variable calculated by counting the rows that exist already.
clonedrow.attr("id",clonedid).insertAfter("#row1");
除了“row1”中的可拖动/可排序事件不会复制到克隆行之外,这一切都有效。我做错了什么吗?我认为通过添加“true”它会复制事件......?
FWIW在加载页面时,我有一个函数可以自动构建第一行,并向其应用可拖动/可排序/删除事件......
I have a row where I can drag items into it, and sort them. This all works ok. I even have a delete event on each item, so it can be removed from the row.
I want an option where I can clone the row. I do this by using the clone function below:
clonedrow = $("#row1").clone(true);
clonedid = "row"+nextRowNumber; //nextRowNumber is a variable calculated by counting the rows that exist already.
clonedrow.attr("id",clonedid).insertAfter("#row1");
This all works, apart from the fact the draggable/sortable events in 'row1' are not copied to the cloned row. Am I doing something wrong? I thought by adding 'true' it copies the events...?
FWIW when loading the page, I have a function which automatically builds the first row, and applies the draggable/sortable/delete events to it...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对draggable不太了解,但是当使用
true
克隆一个draggable元素时,似乎有一些奇怪的行为。也许您不希望克隆所有数据
(如果其中一些数据特定于该元素)。在这个简单的示例中,当您
clone(true)
一个克隆并尝试拖动克隆时,原始版本也会被拖动。http://jsfiddle.net/ZmcHd/
也许最好重新应用
draggable( )
。http://jsfiddle.net/ZmcHd/1/
如果您还需要其他设置,然后我将它们存储在一个变量中以防止重复。
I don't know too much about draggable, but there seems to be some strange behavior when cloning a draggable element with
true
. Perhaps you don't want all thedata
cloned if some of it should be specific to that element.In this simple example, when you
clone(true)
one and try to drag the clone, the original is dragged.http://jsfiddle.net/ZmcHd/
Perhaps it is better to just reapply the
draggable()
.http://jsfiddle.net/ZmcHd/1/
If there are other settings that you need, then I'd store them in a variable in order to prevent duplication.
如果克隆传递 true 参数,则 jQuery 复制事件和数据。为了测试事件是否已被传递,请查看元素数据的 events 属性 http://www.jsfiddle。净/zfSrR/1/
jQuery copy events and data if clone is passed true parameter. In order to test whether events have been passed see events property of element's data http://www.jsfiddle.net/zfSrR/1/