这与问题javascript cloneNode和properties有关。
我看到的是相同的行为。Node.cloneNode不会复制我添加的任何属性(原始帖子中的代码):
var theSource = document.getElementById("someDiv")
theSource.dictator = "stalin";
var theClone = theSource.cloneNode(true);
alert(theClone.dictator);
theClone
不包含任何属性“独裁者”。
对于这种情况,我还没有找到任何解释。关于MDN的文档指出,cloneNode
“复制其所有属性及其值”,这一行直接取自DOM规范本身。
这对我来说似乎很困难,因为它几乎不可能对包含自定义属性的DOM树进行深层复制。
我在这里想念什么吗?
属性不等于属性。
请改用setAttribute()和getAttribute()。
var theSource = document.getElementById("someDiv")
theSource.setAttribute('dictator','stalin');
var theClone = theSource.cloneNode(true);
alert(theClone.getAttribute('dictator'));