为什么cloneNode会排除自定义属性?

Sco*_*ron 5 javascript clonenode properties

这与javascript cloneNode和属性的问题有关.

我看到了同样的行为.Node.cloneNode不会复制我自己添加的任何属性(来自原始帖子的代码):

    var theSource = document.getElementById("someDiv")
    theSource.dictator = "stalin";

    var theClone = theSource.cloneNode(true);
    alert(theClone.dictator); 
Run Code Online (Sandbox Code Playgroud)

theClone 不包含任何属性"独裁者".

我无法找到任何解释为什么会这样.MDN上文档声明cloneNode"复制其所有属性及其值",这一行直接取自DOM规范本身.

这似乎打破了我,因为它几乎不可能做包含自定义属性的DOM树的深层副本.

我在这里错过了什么吗?

Dr.*_*lle 7

属性不等于属性.

请改用setAttribute()和getAttribute().

var theSource = document.getElementById("someDiv")
theSource.setAttribute('dictator','stalin');

var theClone = theSource.cloneNode(true);
alert(theClone.getAttribute('dictator')); 
Run Code Online (Sandbox Code Playgroud)