“警告:在DOM Core 1、2和3中,Attr继承自Node。在DOM4中不再是这种情况。为了使Attr的实现达到规范要求,正在努力将其更改为不再继承Node。您不应该对Attr对象使用任何Node属性或方法。从Gecko7.0(Firefox7.0/ Thunderbird 7.0 / SeaMonkey 2.4)开始,要将其输出警告消息移除到控制台。您应该相应地修改代码。完整列表请参阅废弃的属性和方法。”
向下滚动页面,我们可以看到nodeName和NodeValue的替代品,使用Attr.name和Attr.value。
对于属性或childNodes等其他方法来说,这到底意味着什么?引用说,它是不推荐的,但他们没有给予任何替代!
对于一个属性来说,它是不推荐的,但是对于一个节点也是这样吗?
Attr对象:attr.asp
编辑: nodeValue只对属性( Attr )不推荐,因为Attr将不再从DOM级别4中的节点继承:
下面是一个帮助我理解的简单例子:
<div id="myAttribute">myTextNode</div>
var myDiv = document.getElementById("myAttribute");
// If you want to get "myAttribute" from div tag
alert(myDiv.attributes[0].value);
// Correct way to get value of an attribute (displays "myAttribute")
alert(myDiv.attributes[0].nodeValue);
// Working too but deprecated method for Attr since it doesn't inherit from Node in DOM4 (.nodeValue is specific to a Node, not an Attribute)
// If you want to get "myTextNode" from div tag
alert(myDiv.childNodes[0].value);
// Not working since .value is specific to an attribute, not a Node (displays "undefined")
alert(myDiv.childNodes[0].nodeValue);
// Working, .nodeValue is the correct way to get the value of a Node, it will not be deprecated for Nodes! (displays "myTextNode")也许这将避免在访问属性/节点时对其他人造成混淆:)
发布于 2012-05-08 13:11:58
他们的意思是,曾经是Attr实例的对象(例如Element.getAttributeNode()返回的对象)具有从Node继承的属性。
但是,由于在DOM4中不存在这种情况,所以他们试图删除这种继承。正因为如此,当您现在得到一个Attr对象的实例时,不推荐的列表中列出的属性将按照文档的方式运行。
最大的问题是:对于属性来说是不推荐的,但是对于节点也是这样吗?:不,它们不是被废弃的。您可以从Node文档页面中看到它是自己的具有的属性列表。
Attr对象使用得不多(曾经吗?)不管怎样,你确定这关系到你吗?
https://stackoverflow.com/questions/10499208
复制相似问题