From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/ja/web/api/element/attributes/index.html | 122 +++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 files/ja/web/api/element/attributes/index.html (limited to 'files/ja/web/api/element/attributes') diff --git a/files/ja/web/api/element/attributes/index.html b/files/ja/web/api/element/attributes/index.html new file mode 100644 index 0000000000..f9f8199b7c --- /dev/null +++ b/files/ja/web/api/element/attributes/index.html @@ -0,0 +1,122 @@ +--- +title: Element.attributes +slug: Web/API/Element/attributes +tags: + - API + - Attributes + - DOM + - Element + - Property + - プロパティ + - リファレンス + - 属性 +translation_of: Web/API/Element/attributes +--- +

{{ APIRef("DOM") }}

+ +

Element.attributes プロパティは、そのノードに登録された全ての属性ノードのコレクションを返却します。返却される値は {{domxref("NamedNodeMap")}} であり、 Array ではありません。つまり、 {{jsxref("Array")}} のメソッドは持っておらず、 {{domxref("Attr")}} ノードのインデックスはブラウザーによって変わる可能性があります。より正確に言うと、 attributes はその属性に関するあらゆる情報を表す文字列のキーと値の組です。

+ +

構文

+ +
var attr = element.attributes;
+
+ +

+ +

基本的な例

+ +
// ドキュメント内の最初の <p> 要素を取得する
+var para = document.getElementsByTagName("p")[0];
+var atts = para.attributes;
+ +

要素の属性を列挙する

+ +

ある要素の全ての属性を走査したい時には、インデックスを使うと便利です。
+ 次の例では、 "paragraph" を id に持つ要素の全ての属性ノードを走査し、その属性の値を表示します。

+ +
<!DOCTYPE html>
+
+<html>
+
+ <head>
+  <title>Attributes example</title>
+  <script type="text/javascript">
+   function listAttributes() {
+     var paragraph = document.getElementById("paragraph");
+     var result = document.getElementById("result");
+
+     // まず、最初の段落(p1)がなんらかの属性を持っているか確かめよう
+     if (paragraph.hasAttributes()) {
+       var attrs = paragraph.attributes;
+       var output = "";
+       for(var i = attrs.length - 1; i >= 0; i--) {
+         output += attrs[i].name + "->" + attrs[i].value;
+       }
+       result.value = output;
+     } else {
+       result.value = "No attributes to show";
+     }
+   }
+  </script>
+ </head>
+
+<body>
+ <p id="paragraph" style="color: green;">Sample Paragraph</p>
+ <form action="">
+  <p>
+    <input type="button" value="Show first attribute name and value"
+      onclick="listAttributes();">
+    <input id="result" type="text" value="">
+  </p>
+ </form>
+</body>
+</html>
+ +

仕様書

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
仕様書状態備考
{{SpecName('DOM WHATWG', '#dom-element-attributes', 'Element.attributes')}}{{Spec2('DOM WHATWG')}}{{SpecName('DOM3 Core')}} 以降、 {{domxref("Node")}} から {{domxref("Element")}} へ移動
{{SpecName('DOM3 Core', 'core.html#ID-84CF096', 'Element.attributes')}}{{Spec2('DOM3 Core')}}{{SpecName('DOM2 Core')}} から変更無し
{{SpecName('DOM2 Core', 'core.html#ID-84CF096', 'Element.attributes')}}{{Spec2('DOM2 Core')}}{{SpecName('DOM1')}}から変更無し
{{SpecName('DOM1', 'level-one-core.html#ID-84CF096', 'Element.attributes')}}{{Spec2('DOM1')}}初回定義
+ +

ブラウザーの対応

+ + + +

{{Compat("api.Element.attributes")}}

+ +

関連情報

+ + -- cgit v1.2.3-54-g00ecf