Attr
interface represents one of a DOM element's attributes as an object. In most DOM methods, you will directly retrieve the attribute as a string (e.g.,
Element.getAttribute()
), but certain functions (e.g.,
Element.getAttributeNode()
) or means of iterating return
Attr
类型。
<div id="interfaceDiagram" style="display: inline-block; position: relative; width: 100%; padding-bottom: 11.666666666666666%; vertical-align: middle; overflow: hidden;"><svg style="display: inline-block; position: absolute; top: 0; left: 0;" viewbox="-50 0 600 70" preserveAspectRatio="xMinYMin meet"><a xlink:href="../API/EventTarget.html" target="_top"><rect x="1" y="1" width="110" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text x="56" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">EventTarget</text></a><polyline points="111,25 121,20 121,30 111,25" stroke="#D4DDE4" fill="none"/><line x1="121" y1="25" x2="151" y2="25" stroke="#D4DDE4"/><a xlink:href="../API/Node" target="_top"><rect x="151" y="1" width="75" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text x="188.5" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">Node</text></a><polyline points="226,25 236,20 236,30 226,25" stroke="#D4DDE4" fill="none"/><line x1="236" y1="25" x2="266" y2="25" stroke="#D4DDE4"/><a xlink:href="../API/Attr" target="_top"><rect x="266" y="1" width="75" height="50" fill="#F4F7F8" stroke="#D4DDE4" stroke-width="2px" /><text x="303.5" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">Attr</text></a></svg></div>
a:hover text { fill: #0095DD; pointer-events: all;}
警告:
Starting in Gecko 7.0 (Firefox 7.0 / Thunderbird 7.0 / SeaMonkey 2.4), a number of deprecated properties and methods output warning messages to the console. You should revise your code accordingly. See
Deprecated properties and methods
for a complete list.
名称
只读
The attribute's name.
namespaceURI
只读
DOMString
representing the namespace URI of the attribute, or
null
if there is no namespace.
localName
只读
DOMString
representing the local part of the qualified name of the attribute.
prefix
只读
DOMString
representing the namespace prefix of the attribute, or
null
if no prefix is specified.
ownerElement
只读
The element holding the attribute.
注意:
DOM Level 4 removed this property. The assumption was that since you get an
Attr
object from an
元素
, 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
只读
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
The attribute's value.
注意:
DOM Level 3 defined
namespaceURI
,
localName
and
prefix
在
节点
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 noted.
属性
NULL
.
childNodes
从 Gecko 14 起已过时
NodeList
.
firstChild
从 Gecko 14 起已过时
NULL
.
isId
只读
lastChild
从 Gecko 14 起已过时
NULL
.
nextSibling
NULL
.
nodeName
Attr.name
代替。
nodeType
ATTRIBUTE_NODE
).
nodeValue
Attr.value
代替。
ownerDocument
You shouldn't have been using this in the first place, so you probably don't care that this is going away.
parentNode
NULL
.
previousSibling
NULL
.
schemaTypeInfo
只读
Document.normalizeDocument
, this property may not be reliable if the node was moved.
specified
true
.
textContent
Attr.value
代替。
The following methods have been deprecated:
appendChild()
从 Gecko 14 起已过时
Attr.value
代替。
cloneNode()
You shouldn't have been using this in the first place, so you probably don't care that this is going away.
createAttribute()
Element.setAttribute()
代替。
createAttributeNS()
Element.setAttributeNS()
代替。
getAttributeNode()
Element.getAttribute()
代替。
getAttributeNodeNS()
Element.getAttributeNS()
代替。
hasAttributes()
Obsolete since Gecko 21.0
This method now always returns false.
hasChildNodes()
This method now always returns false.
insertBefore()
Attr.value
代替。
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()
Element.removeAttribute()
代替。
removeChild()
从 Gecko 14 起已过时
Attr.value
代替。
replaceChild()
从 Gecko 14 起已过时
Attr.value
代替。
setAttributeNode()
Element.setAttribute()
代替。
setAttributeNodeNS()
Element.setAttributeNS()
代替。
| 规范 | 状态 | 注释 |
|---|---|---|
|
DOM
The definition of 'Attr' in that specification. |
实时标准 |
添加
ownerElement
property back
|
|
DOM4
The definition of 'Attr' in that specification. |
过时 |
移动
namespaceURI
,
prefix
and
localName
from
节点
to this API and removed
ownerElement
,
schemaTypeInfo
and
isId
.
|
|
DOM (文档对象模型) 3 级核心规范
The definition of 'Attr' in that specification. |
过时 | 初始定义 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
Attr
|
Chrome
1
|
Edge 12 | Firefox 1 | IE 8 |
Opera
8
|
Safari 1.3 |
WebView Android
1
|
Chrome Android
18
|
Firefox Android 4 |
Opera Android
10.1
|
Safari iOS 1 |
Samsung Internet Android
1.0
|
localName
|
Chrome
46
|
Edge ≤18 |
Firefox
48
|
IE No |
Opera
33
|
Safari 1.3 |
WebView Android
46
|
Chrome Android
46
|
Firefox Android
48
|
Opera Android
33
|
Safari iOS 1 |
Samsung Internet Android
5.0
|
namespaceURI
|
Chrome
46
|
Edge ≤18 |
Firefox
48
|
IE No |
Opera
33
|
Safari 1.3 |
WebView Android
46
|
Chrome Android
46
|
Firefox Android
48
|
Opera Android
33
|
Safari iOS 1 |
Samsung Internet Android
5.0
|
prefix
|
Chrome
46
|
Edge ≤18 |
Firefox
48
|
IE No |
Opera
33
|
Safari 1.3 |
WebView Android
46
|
Chrome Android
46
|
Firefox Android
48
|
Opera Android
33
|
Safari iOS 1 |
Samsung Internet Android
5.0
|
完整支持
不支持
见实现注意事项。
Attr
AbortController
AbortSignal
AbstractRange
ByteString
CDATASection
CSSPrimitiveValue
CSSValue
CSSValueList
CharacterData
ChildNode
注释
CustomEvent
DOMConfiguration
DOMError
DOMErrorHandler
DOMException
DOMImplementation
DOMImplementationList
DOMImplementationRegistry
DOMImplementationSource
DOMLocator
DOMObject
DOMParser
DOMPoint
DOMPointInit
DOMPointReadOnly
DOMRect
DOMString
DOMTimeStamp
DOMTokenList
DOMUserData
Document
DocumentFragment
DocumentType
元素
ElementTraversal
Entity
EntityReference
事件
EventTarget
HTMLCollection
MutationObserver
节点
NodeFilter
NodeIterator
NodeList
NonDocumentTypeChildNode
ProcessingInstruction
PromiseResolver
范围
StaticRange
文本
TextDecoder
TextEncoder
TimeRanges
TreeWalker
TypeInfo
USVString
UserDataHandler
XMLDocument