Element.getAttributeNS() - Web APIs 编辑

The getAttributeNS() method of the Element interface returns the string value of the attribute with the specified namespace and name. If the named attribute does not exist, the value returned will either be null or "" (the empty string); see Notes for details.

Syntax

attrVal = element.getAttributeNS(namespace, name)

Parameters

namespace
The namespace in which to look for the specified attribute.
name
The name of the attribute to look for.

Return value

The string value of the specified attribute. If the attribute doesn't exist, the result is null.

Note: Earlier versions of the DOM specification had this method described as returning an empty string for non-existent attributes, but it was not typically implemented this way since null makes more sense. The DOM4 specification now says this method should return null for non-existent attributes.

Examples

The following SVG document reads the value of the foo attribute in a custom namespace.

<svg xmlns="http://www.w3.org/2000/svg"
    xmlns:test="http://www.example.com/2014/test" width="40" height="40">

  <circle id="target" cx="12" cy="12" r="10" stroke="#444"
      stroke-width="2" fill="none" test:foo="Hello namespaced attribute!"/>

  <script type="text/javascript">
    var ns = 'http://www.example.com/2014/test';
    var circle = document.getElementById( 'target' );

    console.log( 'attribute test:foo: "' + circle.getAttributeNS( ns, 'foo' ) + '"' );
  </script>
</svg>

In an HTML5 document the attribute has to be accessed with test:foo since namespaces are not supported.

<!DOCTYPE html>
<html>
<body>

<svg xmlns="http://www.w3.org/2000/svg"
    xmlns:test="http://www.example.com/2014/test" width="40" height="40">
  <circle id="target" cx="12" cy="12" r="10" stroke="#444" stroke-width="2"
      fill="none" test:foo="Foo value"/>
</svg>

<script type="text/javascript">
  var ns = 'http://www.example.com/2014/test';
  var circle = document.getElementById( 'target' );
  console.log('Attribute value: ' + circle.getAttribute('test:foo'));
</script>

</body>
</html>

Notes

Namespaces are only supported in XML documents. HTML5 documents have to use getAttribute() instead.

getAttributeNS() differs from getAttribute() in that it allows you to further specify the requested attribute as being part of a particular namespace, as in the example above, where the attribute is part of the fictional "specialspace" namespace on Mozilla.

Prior to the DOM4 specification, this method was specified to return an empty string rather than null for non-existent attributes. However, most browsers instead returned null. Starting with DOM4, the specification now says to return null. However, some older browsers return an empty string. For that reason, you should use hasAttributeNS() to check for an attribute's existence prior to calling getAttributeNS() if it is possible that the requested attribute does not exist on the specified element.

DOM methods dealing with element's attributes:

Not namespace-aware, most commonly used methodsNamespace-aware variants (DOM Level 2)DOM Level 1 methods for dealing with Attr nodes directly (seldom used)DOM Level 2 namespace-aware methods for dealing with Attr nodes directly (seldom used)
setAttribute (DOM 1)setAttributeNSsetAttributeNodesetAttributeNodeNS
getAttribute (DOM 1)getAttributeNSgetAttributeNodegetAttributeNodeNS
hasAttribute (DOM 2)hasAttributeNS--
removeAttribute (DOM 1)removeAttributeNSremoveAttributeNode-

Specifications

SpecificationStatusComment
DOM
The definition of 'Element.getAttributeNS()' in that specification.
Living Standard 
Document Object Model (DOM) Level 3 Core Specification
The definition of 'Element.getAttributeNS()' in that specification.
ObsoleteSpecifies that a NOT_SUPPORTED_ERR exception is thrown if the UA does not support the "XML" feature. Also specifies that null must be passed to have no namespace.
Document Object Model (DOM) Level 2 Core Specification
The definition of 'Element.getAttributeNS()' in that specification.
ObsoleteInitial definition

Browser compatibility

BCD tables only load in the browser

See also

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

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

发布评论

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

词条统计

浏览:97 次

字数:8162

最后编辑:7 年前

编辑次数:0 次

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