在javascript中设置父节点 [英] setting parent node in javascript

查看:111
本文介绍了在javascript中设置父节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过执行object.cloneNode(true)克隆javascript中的对象时,parentNode在新副本中为null。
我试图设置但没有成功。
我的代码如下所示:

when Im cloning an object in javascript by doing object.cloneNode(true) the parentNode is null in the new copy. Im trying to set it but with no success. my code look like this:

old_DataRoot = DataRoot.cloneNode(true);
old_DataRoot.parentNode=DataRoot.parentNode.cloneNode(true);

也尝试过:

    old_DataRoot = DataRoot.cloneNode(true);
    old_DataRoot.parentNode.appendChild(DataRoot.parentNode.cloneNode(true));

两个选项都给我old_DataRoot.parentNode为null或不是对象
是什么我做错了吗?

both options give me "old_DataRoot.parentNode is null or not an object" what am I doing wrong?

非常感谢,
Yoni。

thanks alot, Yoni.

推荐答案

如果您正在尝试


备份原始DataRoot以便稍后恢复
。 / p>

to make a backup of the original DataRoot in order to recover it later.

然后考虑

// Backup
var DataRootBackup = {
    nodes: DataRoot.cloneNode(true),
    parent: DataRoot.parentNode
};

// Restore
DataRootBackup.parent.appendChild( DataRootBackup.nodes );

这篇关于在javascript中设置父节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆