该类型将DOM元素的属性作为一个对象,在大多数DOM方法中,你可能直接把属性作为字符串取出(例如Element.getAttribute())。但是一些函数(例如Element.getAttributeNode())或迭代的功能会再次返回Attr类型。
Warning:在DOM Core 1、2和3中,Attr继承自Node。在DOM4有所改变。为了规范化Attr的实现,它将不再继承自
Node。此项工作正在进行中,不应该使用任何Attr对象上有关Node的属性和方法。从Gecko 7.0开始(Firefox 7.0 / Thunderbird 7.0 / SeaMonkey 2.4),控制台将输出这些方法和属性将会被移除的警告信息。你应该对代码进行相应的修正。在Deprecated properties and methods可以获取完整的列表。属性
name只读- 该属性的名称
namespaceURI只读- A
DOMStringrepresenting the namespace URI of the attribute, ornullif there is no namespace. localName只读- A
DOMStringrepresenting the local part of the qualified name of the attribute. prefix只读- A
DOMStringrepresenting the namespace prefix of the attribute, ornullif no prefix is specified. ownerElement只读 已废弃 Gecko 7.0 已废弃 Gecko 49.0-
这个元素保存Attr。
注意: DOM Level 4移除了这个方法。由于当你从
Element中获得Attr对象时,你应已知相关的元素。
As that doesn't hold true in cases likeAttrobjects being returned byDocument.evaluate, the DOM Living Standard reintroduced the property.Gecko outputs a deprecation note starting from Gecko 7.0 (Firefox 7.0 / Thunderbird 7.0 / SeaMonkey 2.4). This note was removed again in Gecko 49.0 (Firefox 49.0 / Thunderbird 49.0 / SeaMonkey 2.46).
specified只读- 该属性将返回
真。如果这个属性你在源代码或者在脚本中明确指定的话,它总是返回真。否则它是由文档的DTD默认定义的,将总是返回假。 value- 属性的值
注意: DOM Level 3定义namespaceURI, localName和prefix为Node接口。在DOM4中被移至Attr。
Chrome 46.0版本以上、Firefox 48.0版本以上实现了该改动。
废弃的属性和方法
这些属性已经被废弃,可以使用合适的属性替代。
attributes该属性将返回NULLchildNodes该属性将返回NULLfirstChild该属性将返回NULLisId只读- 指示了该属性是否一个“ID 属性”。“ID 属性”的值在整个DOM文档中应当是唯一。在HTML DOM文档中属性“id”是一个ID属性,也是唯一一个ID属性;但是在XML文档中可以定义其他ID属性。一个属性是否是ID属性,通常在DTD等文档模式描述中定义。
lastChild该属性将返回NULLnextSibling该属性将返回NULLnodeName- 使用
Attr.name来代替 nodeType- 该属性将返回2,表示
ATTRIBUTE_NODE nodeValue- 使用
Attr.value来代替 ownerDocument- 这个属性本不应当在这里被使用,所以应该无须担心其演变
parentNode该属性将返回NULLpreviousSibling该属性将返回NULLschemaTypeInfo只读- The type information associated with this attribute. While the type information contained in this attribute is guaranteed to be correct after loading the document or invoking
Document.normalizeDocument, this property may not be reliable if the node was moved. specified该属性将返回truetextContent- 使用
Attr.value来代替
这些方法已经被废弃:
appendChild()已废弃 Gecko 14- 通过编辑
Attr.value属性来实现相同的效果 cloneNode()- 这个方法本不应当在这里被使用,所以无须担心其演变
createAttribute()- 使用
Element.setAttribute()来代替 createAttributeNS()- 使用
Element.setAttributeNS()来代替 getAttributeNode()- 使用
Element.getAttribute()来代替 getAttributeNodeNS()- 使用
Element.getAttributeNS()来代替 hasAttributes()已废弃 Gecko 21.0- 该方法将返回false.
hasChildNodes()- 该方法将返回false.
insertBefore()- 通过编辑
Attr.value来实现相同效果 isSupported()- 这个方法本不应当被在这里使用,所以无须担心其演变
isEqualNode()- 这个方法本不应当被在这里使用,所以无须担心其演变
normalize()- 这个方法本不应当被在这里使用,所以无须担心其演变
removeAttributeNode()- 使用
Element.removeAttribute()来代替 removeChild()- 通过编辑
Attr.value来实现相同效果 replaceChild()- 通过编辑
Attr.value来实现相同效果 setAttributeNode()- 使用
Element.setAttribute()来代替 setAttributeNodeNS()- 使用
Element.setAttributeNS()来代替
规格
| 规格 | 状态 | 注释 |
|---|---|---|
| DOM Attr |
Living Standard | 加回 ownerElement 属性 |
| DOM4 Attr |
Obsolete | 将 namespaceURI、prefix 和 localName从 Node 移至本API,且删除 ownerElement、schemaTypeInfo 和 isId. |
| Document Object Model (DOM) Level 3 Core Specification Attr |
Obsolete | 初始定义 |
浏览器兼容性
| Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|---|
| 基本支持 | (Yes)[1] | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| Feature | Android | Android Webview | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|---|---|
| 基本支持 | (Yes) | (Yes)[1] | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes)[1] |
[1] As of Chrome 45, this property no longer inherits from Node.