这与问题相关javascript cloneNode 和属性 https://stackoverflow.com/questions/4094811/javascript-clonenode-and-properties.
我看到同样的行为。 Node.cloneNode 不会复制我自己添加的任何属性(来自原始帖子的代码):
var theSource = document.getElementById("someDiv")
theSource.dictator = "stalin";
var theClone = theSource.cloneNode(true);
alert(theClone.dictator);
theClone
不包含任何属性“独裁者”。
我无法找到任何解释为什么会出现这种情况。这MDN 上的文档 https://developer.mozilla.org/en-US/docs/DOM/Node.cloneNode指出cloneNode
“复制其所有属性及其值”,直接从DOM规范 http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-3A0ED0A4 itself.
这对我来说似乎很糟糕,因为它几乎不可能对包含自定义属性的 DOM 树进行深层复制。
我在这里错过了什么吗?
属性不等于属性。
请改用 setAttribute() 和 getAttribute()。
var theSource = document.getElementById("someDiv")
theSource.setAttribute('dictator','stalin');
var theClone = theSource.cloneNode(true);
alert(theClone.getAttribute('dictator'));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)