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/node/childnodes/index.html | 103 ++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 files/ja/web/api/node/childnodes/index.html (limited to 'files/ja/web/api/node/childnodes') diff --git a/files/ja/web/api/node/childnodes/index.html b/files/ja/web/api/node/childnodes/index.html new file mode 100644 index 0000000000..27c39be8d3 --- /dev/null +++ b/files/ja/web/api/node/childnodes/index.html @@ -0,0 +1,103 @@ +--- +title: Node.childNodes +slug: Web/API/Node/childNodes +tags: + - API + - DOM + - DOMリファレンス + - プロパティ + - リファレンス +translation_of: Web/API/Node/childNodes +--- +

{{ ApiRef() }}

+ +

Node.childNodes読み取り専用プロパティは、最初の子ノードにインデックス0が割り当てられている、指定された要素の子{{domxref("Node","nodes")}}の現在の{{domxref("NodeList")}}を返します。

+ +

構文

+ +
let nodeList = elementNodeReference.childNodes;
+
+ +

+ +

簡単な使用方法

+ +
// 変数pargは<p>要素へのオブジェクト参照です
+
+// まず、pargが子ノードを持っているかをチェックします
+if (parg.hasChildNodes()) {
+  var children = parg.childNodes;
+
+  for (let i = 0; i < children.length; i++) {
+    // for文を使って各ノードにchildren[i]としてアクセスします
+    // 注意! NodeListは不変ではないので、ノードを追加したり削除したりするとchildren.lengthは変化します
+  }
+}
+ +

ノードから全ての子を削除する

+ +
// これは一例ですが、この方法でノードからすべての子ノードを削除することができます
+// let box = document.getElementById(/**/);
+
+while (box.firstChild) {
+    // NodeListは不変ではないので、毎処理ごとにbox.firstChildは変化します
+    box.removeChild(box.firstChild);
+}
+ +

注記

+ +

ノードのコレクション内の項目はオブジェクトであり、文字列ではありません。node オブジェクトからデータを取得するには、そのプロパティ (たとえばelementNodeReference.childNodes[1].nodeName で名前を取得) を使用します。
+
+ documentオブジェクト自体には Doctype 宣言と root 要素 の2つの子があり、通常はdocumentElementと呼ばれます。 ((X)HTML文書ではこれがHTML要素です)
+
+ childNodesにはテキストノードやコメントノードなどの非要素ノードを含むすべての子ノードが含まれます。要素のみのコレクションを取得するには、代わりに{{domxref("ParentNode.children")}} を使用してください。

+ +

仕様

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
仕様ステータスコメント
{{SpecName('DOM WHATWG', '#dom-node-childnodes', 'Node.childNodes')}}{{Spec2('DOM WHATWG')}}変更なし
{{SpecName('DOM3 Core', 'core.html#ID-1451460987', 'Node.childNodes')}}{{Spec2('DOM3 Core')}}変更なし
{{SpecName('DOM2 Core', 'core.html#ID-1451460987', 'Node.childNodes')}}{{Spec2('DOM2 Core')}}変更なし
{{SpecName('DOM1', 'level-one-core.html#ID-1451460987', 'Node.childNodes')}}{{Spec2('DOM1')}}初回定義
+ +

ブラウザの互換性

+ + + +

{{Compat("api.Node.childNodes")}}

+ +

関連情報

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