Movatterモバイル変換


[0]ホーム

URL:


  1. 開発者向けのウェブ技術
  2. Web API
  3. Element
  4. getAttributeNS()

このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docsコミュニティーについてもっと知り、仲間になるにはこちらから。

View in EnglishAlways switch to English

Element: getAttributeNS() メソッド

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.

getAttributeNS()Element インターフェイスのメソッドで、指定された名前空間と名前を持つ属性の文字列値を返します。のような名前の属性が存在しない場合は、null または"" (空文字列のどちらかを返します。詳しくはメモを参照してください。

構文

js
getAttributeNS(namespace, name)

引数

namespace

指定された属性を探す名前空間です。

name

探す属性の名前です。

返値

指定された属性の文字列値です。その属性が存在しない場合、結果はnull になります。

メモ:古いバージョンの DOM 仕様書では、このメソッドが存在しない属性に対しては空文字列を返すと説明していました。しかし、 null の方が分かりやすいので、そのような実装はあまり行われませんでした。 DOM4 仕様書ではこのメソッドは存在しない属性に対して null を返すと書くようになりました。

以下の SVG 文書は独自の名前空間にあるfoo 属性の値を読み取ります。

xml
<svg xmlns="http://www.w3.org/2000/svg"    xmlns:test="http://www.example.com/2014/test" width="40" height="40">  <circle cx="12" cy="12" r="10" stroke="#444"      stroke-width="2" fill="none" test:foo="Hello namespaced attribute!"/>  <script>    const ns = 'http://www.example.com/2014/test';    const circle = document.getElementById('target');    console.log(`attribute test:foo: "${circle.getAttributeNS(ns, 'foo')}"`);  </script></svg>

HTML 文書では名前空間に対応していないため、この属性はtest:foo でアクセスする必要があります。

html
<!doctype html><html lang="en-US">  <head>    <meta charset="UTF-8" />    <title>getAttributeNS() test page</title>  </head>  <body>    <svg      xmlns="http://www.w3.org/2000/svg"      xmlns:test="http://www.example.com/2014/test"      width="40"      height="40">      <circle               cx="12"        cy="12"        r="10"        stroke="#444"        stroke-width="2"        fill="none"        test:foo="Foo value" />    </svg>    <script>      const ns = "http://www.example.com/2014/test";      const circle = document.getElementById("target");      console.log(`Attribute value: ${circle.getAttribute("test:foo")}`);    </script>  </body></html>

メモ

名前空間は XML 文書でのみ対応しています。 HTML 文書では、代わりにgetAttribute() を使用する必要があります。

getAttributeNS()getAttribute() とは異なり、特定の名前空間に属している要求された属性をより深く特定することができます。上記の例では、属性は Mozilla の架空の "specialspace" 名前空間に属しています。

DOM4 より前の仕様では、このメソッドは属性が存在しない場合に null ではなく空文字列を返すように指定されていました。しかし、ほとんどのウェブブラウザーは null を返していました。 DOM4 以降は、仕様でも null を返すように指定されました。しかし、一部の古いウェブブラウザーは空文字列を返します。そのため、指定の要素に指定の属性が存在しない可能性があるなら、getAttributeNS を呼ぶ前にhasAttributeNS() を使用して属性の存在を確かめる必要があります。

仕様書

Specification
DOM
# ref-for-dom-element-getattributens①

ブラウザーの互換性

関連情報

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp