javascript:克隆对象及其节点
我似乎无法在其他问题中找到我想要的东西。我想知道是否有办法克隆一个对象及其 DOM 节点。我已经尝试过:
newObj = jQuery.extend(true, {}, oldObj);
但这不会克隆任何节点,并且:
newObj = oldObj.cloneNode(true);
但这不会克隆为克隆节点提供功能并存储与节点相关的大部分信息的对象。我的目的是在页面加载时定义原始对象,然后克隆它并隐藏它,然后能够稍后再次克隆该对象。
我考虑过只克隆 DOM 节点,然后每次重新创建对象,但该对象很大,需要大量代码来定义其所有属性和方法。
I couldn't seem to find quite what I'm looking for in any other question. I am wondering if there is a way to clone an object and its DOM nodes. I have tried:
newObj = jQuery.extend(true, {}, oldObj);
but this does not clone any of the nodes, and :
newObj = oldObj.cloneNode(true);
but this does not clone the object that gives the cloned nodes their functionality and stores a large part of information pertaining to the nodes. My intent was to have the original object defined when the page loads, then clone it and hide it, and then be able to clone that object later again.
I have considered just cloning the DOM nodes and then recreating the object each time, but the object is large and takes a lot of code to define all of its properties and methods.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需使用
$(element).clone();
Simply use
$(element).clone();
正确的 jQuery 方法:
这直接来自 马嘴 - 马是John Resig</a>。
The proper jQuery way:
And that's straight from the horse's mouth - the horse being John Resig.