本文介绍了jQuery复制/追加元素,已删除基础对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将基本元素 li.table-field-base 克隆为变量 clonedItem ,更改新元素的某些属性和数据,然后将其附加到可排序列表 ul#tablefield-order-sortable .结果是该元素被追加,但是基本元素被删除,尽管我确实首先复制了它.我如何保留基本元素,以便以后可以再次使用它?

Trying to clone base element li.table-field-base to variable clonedItem, change some attributes and data on the new element and then append it to sortable list ul#tablefield-order-sortable. The result is that the element is appended, but the base element is removed, though i did copy it first. How could i keep the base element, so i can use it later again?

以下是复制/附加部分的相关代码段:

Following is the piece of relevant code that does the copying/appending part:

    var clonedItem = $.extend({}, $('li.table-field-base'));
    $('#tablefield-order-sortable').append(clonedItem);
    $('#tablefield-order-sortable').sortable();

先谢谢大家!

推荐答案

您并没有真正克隆元素,而是克隆了包含该元素的对象.
要在jQuery中克隆DOM节点,请使用 clone()

You didn't really clone the element, you cloned the object containing the element.
To clone DOM nodes in jQuery, you use clone()

var clonedItem = $('li.table-field-base').clone();
$('#tablefield-order-sortable').append(clonedItem);
$('#tablefield-order-sortable').sortable();

这篇关于jQuery复制/追加元素,已删除基础对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 04:53