MDN’s new design is in Beta! A sneak peek: https://blog.mozilla.org/opendesign/mdns-new-design-beta/

翻譯不完整。請協助 翻譯此英文文件

DOM 元素的屬性(attribute)會被視作為 Attr 介面的物件(屬性節點)。在大部分的 DOM 方法中,可能是直接以字串型式取得屬性(如 Element.getAttribute())。但在某些方法(如 Element.getAttributeNode()),或於迭代中則會是回傳 Attr 型別。

Warning: In DOM Core 1, 2 and 3, Attr inherited from Node. This is no longer the case in DOM4. In order to bring the implementation of Attr up to specification, work is underway to change it to no longer inherit from Node. You should not be using any Node properties or methods on Attr objects.
Starting in Gecko 7.0 (Firefox 7.0 / Thunderbird 7.0 / SeaMonkey 2.4), the ones that are going to be removed output warning messages to the console. You should revise your code accordingly. See Deprecated properties and methods for a complete list.

屬性

name Read only
屬性名稱。
namespaceURI Read only
一個 DOMString 型別的物件,表示屬性的 namespace URI。若沒有 namespace 則會是 null
localName Read only
一個 DOMString 型別的物件,representing the local part of the qualified name of the attribute.
prefix Read only
一個 DOMString 型別的物件,representing the namespace prefix of the attribute, or null if no prefix is specified.
ownerElement Read only

The element holding the attribute.

Note: DOM Level 4 removed this property. The assumption was that since you get an Attr object from an Element, you should already know the associated element.
As that doesn't hold true in cases like Attr objects being returned by Document.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 Read only
This property always returns true. Originally, it returned true if the attribute was explicitly specified in the source code or by a script, and false if its value came from the default one defined in the document's DTD.
value
屬性值。

Note: DOM Level 3 defined namespaceURI, localName and prefix on the Node interface. In DOM4 they were moved to Attr.

This change is implemented in Chrome since version 46.0 and Firefox since version 48.0.

過時屬性與方法

The following properties have been deprecated. Where available, the appropriate replacement is provided.

attributes
This property now always returns NULL.
childNodes 已過時 Gecko 14
This property now always returns and empty NodeList.
firstChild 已過時 Gecko 14
This property now always returns NULL.
isId Read only
Indicates whether the attribute is an "ID attribute". An "ID attribute" being an attribute which value is expected to be unique across a DOM Document. In HTML DOM, "id" is the only ID attribute, but XML documents could define others. Whether or not an attribute is unique is often determined by a DTD or other schema description.
lastChild 已過時 Gecko 14
This property now always returns NULL.
nextSibling
This property now always returns NULL.
nodeName
Use Attr.name instead.
nodeType
This property now always returns 2 (ATTRIBUTE_NODE).
nodeValue
Use Attr.value instead.
ownerDocument
You shouldn't have been using this in the first place, so you probably don't care that this is going away.
parentNode
This property now always returns NULL.
previousSibling
This property now always returns NULL.
schemaTypeInfo Read only
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
This property now always returns true.
textContent
Use Attr.value instead.

The following methods have been deprecated:

appendChild() 已過時 Gecko 14
Modify the value of Attr.value instead.
cloneNode()
You shouldn't have been using this in the first place, so you probably don't care that this is going away.
createAttribute()
Use Element.setAttribute() instead.
createAttributeNS()
Use Element.setAttributeNS() instead.
getAttributeNode()
Use Element.getAttribute() instead.
getAttributeNodeNS()
Use Element.getAttributeNS() instead.
hasAttributes() 已過時 Gecko 21.0
This method now always returns false.
hasChildNodes()
This method now always returns false.
insertBefore()
Modify the value of Attr.value instead.
isSupported()
You shouldn't have been using this in the first place, so you probably don't care that this is going away.
isEqualNode()
You shouldn't have been using this in the first place, so you probably don't care that this is going away.
normalize()
You shouldn't have been using this in the first place, so you probably don't care that this is going away.
removeAttributeNode()
Use Element.removeAttribute() instead.
removeChild() 已過時 Gecko 14
Modify the value of Attr.value instead.
replaceChild() 已過時 Gecko 14
Modify the value of Attr.value instead.
setAttributeNode()
Use Element.setAttribute() instead.
setAttributeNodeNS()
Use Element.setAttributeNS() instead.

規範

Specification Status Comment
DOM
The definition of 'Attr' in that specification.
Living Standard Added ownerElement property back
DOM4
The definition of 'Attr' in that specification.
Recommendation Moved namespaceURI, prefix and localName from Node to this API and removed ownerElement, schemaTypeInfo and isId.
Document Object Model (DOM) Level 3 Core Specification
The definition of 'Attr' in that specification.
Recommendation Initial definition

瀏覽器相容性

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes)[1] (Yes) (Yes) (Yes) (Yes) (Yes)
Feature Android Android Webview Edge Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support (Yes) (Yes)[1] (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)[1]

[1] As of Chrome 45, this property no longer inherits from Node.

文件標籤與貢獻者

 此頁面的貢獻者: jackblackevo
 最近更新: jackblackevo,