JavaScript   发布时间:2022-04-16  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了javascript – 为什么cloneNode会排除自定义属性?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

这与问题javascript cloneNode and properties有关.

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

    var thesource = document.getElementById("someDiv")
    thesource.Dictator = "stalin";

    var theClone = thesource.cloneNode(true);
    alert(theClone.Dictator); 

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

我无法找到任何解释为什么会这样. documentation on MDN声明cloneNode“复制其所有属性及其值”,这条线直接取自DOM specification本身.

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

我在这里错过了什么吗?

最佳答案
属性不等于属性.

请改用setAttribute()和getAttribute().

var thesource = document.getElementById("someDiv")
thesource.setAttribute('Dictator','stalin');

var theClone = thesource.cloneNode(true);
alert(theClone.getAttribute('Dictator')); 

大佬总结

以上是大佬教程为你收集整理的javascript – 为什么cloneNode会排除自定义属性?全部内容,希望文章能够帮你解决javascript – 为什么cloneNode会排除自定义属性?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。