Attr - Web APIs 编辑

The 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 types.

  <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="/wiki/en-US/docs/Web/API/EventTarget" 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="https://developer.mozilla.org/wiki/en-US/docs/Web/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="/wiki/en-US/docs/Web/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;}
Warning: 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.

Properties

name Read only
The attribute's name.
namespaceURI Read only
A DOMString representing the namespace URI of the attribute, or null if there is no namespace.
localName Read only
A DOMString representing the local part of the qualified name of the attribute.
prefix Read only
A 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 trueif 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.

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.

Deprecated properties and methods

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

attributes
This property now always returns NULL.
childNodes Obsolete since Gecko 14
This property now always returns an empty NodeList.
firstChild Obsolete since 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 Obsolete since 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 This is an obsolete API and is no longer guaranteed to work. 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() Obsolete since 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() Obsolete since 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() Obsolete since Gecko 14
Modify the value of Attr.value instead.
replaceChild() Obsolete since Gecko 14
Modify the value of Attr.value instead.
setAttributeNode()
Use Element.setAttribute() instead.
setAttributeNodeNS()
Use Element.setAttributeNS() instead.

Specifications

SpecificationStatusComment
DOM
The definition of 'Attr' in that specification.
Living StandardAdded ownerElement property back
DOM4
The definition of 'Attr' in that specification.
ObsoleteMoved 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.
ObsoleteInitial definition

Browser compatibility

BCD tables only load in the browser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:55 次

字数:15904

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文