diff options
| author | Masahiro FUJIMOTO <mfujimot@gmail.com> | 2022-03-02 23:26:56 +0900 |
|---|---|---|
| committer | Masahiro FUJIMOTO <mfujimot@gmail.com> | 2022-03-10 22:56:58 +0900 |
| commit | 78061aaf185f3c864174650a7b3c0dbff2a87958 (patch) | |
| tree | 26088426a9de85c22468a0e82ecbd652649dfc55 /files/ja/web/api/element/getattributenames | |
| parent | 7d9629e268d787683dbb5e29e228b3cb0a20e6ad (diff) | |
| download | translated-content-78061aaf185f3c864174650a7b3c0dbff2a87958.tar.gz translated-content-78061aaf185f3c864174650a7b3c0dbff2a87958.tar.bz2 translated-content-78061aaf185f3c864174650a7b3c0dbff2a87958.zip | |
2022/02/18 時点の英語版に同期
Diffstat (limited to 'files/ja/web/api/element/getattributenames')
| -rw-r--r-- | files/ja/web/api/element/getattributenames/index.md | 102 |
1 files changed, 59 insertions, 43 deletions
diff --git a/files/ja/web/api/element/getattributenames/index.md b/files/ja/web/api/element/getattributenames/index.md index f353b84b74..7aa98aca5b 100644 --- a/files/ja/web/api/element/getattributenames/index.md +++ b/files/ja/web/api/element/getattributenames/index.md @@ -3,68 +3,84 @@ title: Element.getAttributeNames() slug: Web/API/Element/getAttributeNames tags: - API + - 属性 - DOM - Element - - getAttributeNames - メソッド - - 属性 + - getAttributeNames +browser-compat: api.Element.getAttributeNames translation_of: Web/API/Element/getAttributeNames --- -<div>{{APIRef("DOM")}}</div> +{{APIRef("DOM")}} + +**`getAttributeNames()`** は {{domxref("Element")}} インターフェースのメソッドで、この要素の属性名を文字列の {{jsxref("Array")}} で返します。要素に属性がない場合は、空の配列を返します。 + +`getAttributeNames()` を {{domxref("Element.getAttribute","getAttribute()")}} と共に使用すると、 {{domxref("Element.attributes")}} にアクセスするよりメモリ効率やパフォーマンスが良くなります。 + +**`getAttributeNames()`** から返される名前は*修飾*属性名です。すなわち、名前空間接頭辞がついた属性であり、名前空間接頭辞(実際の名前空間では*ない*)にコロンが続き、属性名が続きます(例えば **`xlink:href`**)。名前空間接頭辞のない属性は、そのままの名前になります(例えば **`href`**)。 + +## 構文 + +```js +let attributeNames = element.getAttributeNames(); +``` -<p>{{domxref("Element")}} インターフェースの <strong><code>getAttributeNames()</code></strong> メソッドは要素の属性の名前を文字列の {{jsxref("Array")}} で返します。要素に属性がない場合は、空の配列を返します。</p> +## 例 -<p><code>getAttributeNames()</code> を {{domxref("Element.getAttribute","getAttribute()")}} と共に使用すると、 {{domxref("Element.attributes")}} にアクセスするよりメモリ効率やパフォーマンスが良くなります。</p> +以下の例は、次の方法を示しています。 -<h2 id="Syntax" name="Syntax">構文</h2> +- 名前空間接頭辞のある属性については、 `getAttributeNames()` は属性名と一緒に名前空間接頭辞を返します。 +- 名前空間接頭辞のない属性については、 `getAttributeNames()` は属性名のみをそのまま返します。 -<pre class="syntaxbox notranslate"><em>let attributeNames</em> = element.getAttributeNames(); -</pre> +以下のことを理解することが重要です。 -<h2 id="Example" name="Example">例</h2> +1. DOM には名前空間に所属していても、名前空間接頭辞がない場合があります。 +2. 名前空間に所属しているが、名前空間接頭辞のない DOM 内の属性については、 `getAttributeNames()` は属性名だけを返し、その属性が名前空間に所属していることを示しません。 -<pre class="brush:js notranslate">// 要素の属性に対して反復処理する -for(let name of element.getAttributeNames()) { - let value = element.getAttribute(name); - console.log(name, value); +以下の例では、このような「名前空間に所属しているが、名前空間接頭辞がない」場合を示しています。 + +```js +const element = document.createElement('a') + +// "href" 属性を名前空間なし、名前空間接頭辞なしで設定 +element.setAttribute('href', 'https://example.com') +// "href" 属性を名前空間あり、 "xlink" 名前空間接頭辞で設定 +element.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', 'https://example.com') +// "show" 属性を名前空間あり、名前空間接頭辞なしで設定 +element.setAttributeNS('http://www.w3.org/1999/xlink', 'show', 'new') + +// 要素の属性を反復処理する +for (let name of element.getAttributeNames()) { + let value = element.getAttribute(name); + console.log(name, value); } -</pre> -<h2 id="Polyfill" name="Polyfill">代替モジュール</h2> +// 出力結果: +// href https://example.com +// xlink:href https://example.com +// show new +``` -<pre class="brush:js notranslate">if (Element.prototype.getAttributeNames == undefined) { +## ポリフィル + +```js +if (Element.prototype.getAttributeNames == undefined) { Element.prototype.getAttributeNames = function () { var attributes = this.attributes; var length = attributes.length; var result = new Array(length); - for (var i = 0; i < length; i++) { + for (var i = 0; i < length; i++) { result[i] = attributes[i].name; } return result; }; -}</pre> - -<h2 id="Specifications" name="Specifications">仕様書</h2> - -<table class="standard-table"> - <thead> - <tr> - <th scope="col">仕様書</th> - <th scope="col">状態</th> - <th scope="col">備考</th> - </tr> - </thead> - <tbody> - <tr> - <td>{{SpecName("DOM WHATWG", "#dom-element-getattributenames", "Element.getAttributeNames")}}</td> - <td>{{Spec2("DOM WHATWG")}}</td> - <td>初回定義</td> - </tr> - </tbody> -</table> - -<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの対応</h2> - -<div> -<p>{{Compat("api.Element.getAttributeNames")}}</p> -</div> +} +``` + +## 仕様書 + +{{Specifications}} + +## ブラウザーの互換性 + +{{Compat}} |
