From 7d9629e268d787683dbb5e29e228b3cb0a20e6ad Mon Sep 17 00:00:00 2001 From: Masahiro FUJIMOTO Date: Wed, 2 Mar 2022 23:08:13 +0900 Subject: 2021/09/15 時点の英語版に同期 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- files/ja/web/api/element/getattributens/index.md | 107 +++++++++++++++++------ 1 file changed, 80 insertions(+), 27 deletions(-) (limited to 'files/ja/web/api/element/getattributens') diff --git a/files/ja/web/api/element/getattributens/index.md b/files/ja/web/api/element/getattributens/index.md index ee03de1cab..7ec8113595 100644 --- a/files/ja/web/api/element/getattributens/index.md +++ b/files/ja/web/api/element/getattributens/index.md @@ -1,47 +1,100 @@ --- -title: element.getAttributeNS +title: Element.getAttributeNS() slug: Web/API/Element/getAttributeNS tags: + - API - DOM - - Gecko - - 翻訳中 + - Element + - メソッド + - リファレンス +browser-compat: api.Element.getAttributeNS translation_of: Web/API/Element/getAttributeNS --- -

{{ ApiRef("DOM") }}

+{{APIRef("DOM")}} -

概要

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

getAttributeNS は指定の名前空間と名前の属性の文字列値を返します。もし指定の名前の属性が存在しなければ、戻り値は null または ""(空文字列) のいずれかとなります。詳細は{{ Anch("注記") }} を参照。

+## 構文 -

構文

+```js +attrVal = element.getAttributeNS(namespace, name) +``` -
attrVal =element.getAttributeNS(namespace,name)
-
+### 引数 -

引数

+- `namespace` + - : 指定された属性を探す名前空間です。 +- `name` + - : 探す属性の名前です。 - +### 返値 -

+指定された属性の文字列値です。その属性が存在しない場合、結果は `null` になります。 -
var div1 = document.getElementById("div1");
-var a = div1.getAttributeNS("www.mozilla.org/ns/specialspace/",
-                            "special-align");
-alert(a); // div の align 属性の値を表示します。
-
+> **Note:** 古いバージョンの DOM 仕様書では、このメソッドが存在しない属性に対しては空文字列を返すと説明していました。しかし、 null の方が分かりやすいので、そのような実装はあまり行われませんでした。 DOM4 仕様書ではこのメソッドは存在しない属性に対して null を返すと書くようになりました。 -

注記

+## 例 -

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

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

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

+```xml + -

{{ DOMAttributeMethods() }}

+ -

仕様

+ + +``` + +HTML5 文書では名前空間に対応していないため、この属性は `test:foo` でアクセスする必要があります。 + +```html + + + + + + + + + + + + +``` + +## メモ + +名前空間は XML 文書でのみ対応しています。 HTML5 文書では、代わりに `getAttribute()` を使用する必要があります。 + +`getAttributeNS()` は {{domxref("element.getAttribute()", + "getAttribute()")}} とは異なり、特定の名前空間に属している要求された属性をより深く特定することができます。上記の例では、属性は Mozilla の架空の "specialspace" 名前空間に属しています。 + +DOM4 より前の仕様では、このメソッドは属性が存在しない場合に null ではなく空文字列を返すように指定されていました。しかし、ほとんどのウェブブラウザーは null を返していました。 DOM4 以降は、仕様でも null を返すように指定されました。しかし、一部の古いウェブブラウザーは空文字列を返します。そのため、指定の要素に指定の属性が存在しない可能性があるなら、 `getAttributeNS` を呼ぶ前に {{domxref("element.hasAttributeNS()", "hasAttributeNS()")}} を使用して属性の存在を確かめる必要があります。 + +{{DOMAttributeMethods}} + +## 仕様書 + +{{Specifications}} + +## ブラウザーの互換性 + +{{Compat}} + +## 関連情報 + +- [コードスニペット:getAttributeNS](/ja/docs/Mozilla/Add-ons/Code_snippets/getAttributeNS) -- cgit v1.2.3-54-g00ecf