有没有一种快速的方法来“超级”深度克隆一个节点,包括它的属性?(我想还有方法)
var theSource = document.getElementById("someDiv") theSource.dictator = "stalin"; var theClone = theSource.cloneNode(true); alert(theClone.dictator);
新克隆的对象没有 dictator 财产。现在,假设我有一千处房产 theSource
dictator
theSource
//编辑
@法布里齐奥
你的 hasOwnProperty
hasOwnProperty
temp = obj.cloneNode(true); for(p in obj) { if(obj.hasOwnProperty(p)) { eval("temp."+p+"=obj."+p); } }
保存许多属性的最好方法可能是创建一个属性对象,在该对象中可以存储所有属性,例如。
thesource.myproperties = {} thesource.myproperties.dictator1 = "stalin"; thesource.myproperties.dictator2 = "ceasescu"; thesource.myproperties.dictator3 = "Berlusconi"; ...
那你只能复制一个属性
theclone.myproperties = thesource.myproperties
for 为已存储的所有属性循环
for
for (p in thesource) { if (thesource.hasOwnProperty(p)) { theclone.p = thesource.p; } }