jquery 克隆似乎不保留可拖动/可放置事件

发布于 2024-09-16 09:06:00 字数 475 浏览 4 评论 0原文

我有一排可以将项目拖入其中并对它们进行排序。这一切都正常。我什至对每个项目都有一个删除事件,因此可以将其从行中删除。

我想要一个可以克隆行的选项。我通过使用下面的克隆函数来完成此操作:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悟红尘 2024-09-23 09:06:00

我对draggable不太了解,但是当使用true克隆一个draggable元素时,似乎有一些奇怪的行为。也许您希望克隆所有数据(如果其中一些数据特定于该元素)。

在这个简单的示例中,当您clone(true)一个克隆并尝试拖动克隆时,原始版本也会被拖动。

http://jsfiddle.net/ZmcHd/

也许最好重新应用 draggable( )

clonedrow = $("#row1").clone().draggable();

http://jsfiddle.net/ZmcHd/1/

如果您还需要其他设置,然后我将它们存储在一个变量中以防止重复。

var settings = {
        // some settings
}

clonedrow = $("#row1").clone().draggable(settings);

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 the data 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().

clonedrow = $("#row1").clone().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.

var settings = {
        // some settings
}

clonedrow = $("#row1").clone().draggable(settings);
若言繁花未落 2024-09-23 09:06:00

如果克隆传递 true 参数,则 jQuery 复制事件和数据。为了测试事件是否已被传递,请查看元素数据的 events 属性 http://www.jsfiddle。净/zfSrR/1/

console.log($("#row1").data('events'));
console.log($(clonedrow).data('events'));

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/

console.log($("#row1").data('events'));
console.log($(clonedrow).data('events'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文