为什么cloneNode排除自定义属性? [英] Why does cloneNode exclude custom properties?

查看:81
本文介绍了为什么cloneNode排除自定义属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我看到的是相同的行为. Node.cloneNode不会复制我添加的任何属性(原始帖子中的代码):

I'm seeing the same behaviour. Node.cloneNode does not copy over any properties that I add myself (code from original post):

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

    var theClone = theSource.cloneNode(true);
    alert(theClone.dictator); 

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

theClone does not contain any property "dictator".

对于这种情况,我无法找到任何解释. MDN上的文档指出,cloneNode已全部复制的属性及其值",直接从 DOM规范本身.

I haven't been able to find any explanation for why this is the case. The documentation on MDN states that cloneNode "copies all of its attributes and their values", a line which is taken directly from the DOM specification itself.

这对我来说似乎很麻烦,因为它几乎不可能对包含自定义属性的DOM树进行深层复制.

This seems broken to me as it makes it next to impossible to do a deep copy of a DOM tree that contains custom properties.

我在这里想念东西吗?

推荐答案

属性不等于属性.

请改用setAttribute()和getAttribute().

Use setAttribute() and getAttribute() instead.

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

var theClone = theSource.cloneNode(true);
alert(theClone.getAttribute('dictator')); 

这篇关于为什么cloneNode排除自定义属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆