diff options
Diffstat (limited to 'files/zh-tw/web/api/node/childnodes')
| -rw-r--r-- | files/zh-tw/web/api/node/childnodes/index.html | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/files/zh-tw/web/api/node/childnodes/index.html b/files/zh-tw/web/api/node/childnodes/index.html new file mode 100644 index 0000000000..a828bd781b --- /dev/null +++ b/files/zh-tw/web/api/node/childnodes/index.html @@ -0,0 +1,66 @@ +--- +title: Node.childNodes +slug: Web/API/Node/childNodes +translation_of: Web/API/Node/childNodes +--- +<div> +<div>{{APIRef("DOM")}}</div> +</div> + +<p><code><strong>Node.childNodes</strong></code> 唯讀屬性會回傳一個<em>即時更新的動態集合(live collection)</em>,其包含了指定元素的子{{domxref("Node","節點")}},而第一個子節點會被指派為索引 0。</p> + +<h2 id="Syntax" name="Syntax">語法</h2> + +<pre class="syntaxbox">var <var>ndList</var> = elementNodeReference.childNodes; +</pre> + +<p><var>ndList</var> 是一個 {{domxref("NodeList")}},為一個有順序性、由目前元素之 DOM 子節點組成之集合。假如目前元素沒有子節點,則 <var>ndList</var> 會是空的。</p> + +<p>範例</p> + +<pre class="brush:js">// parg is an object reference to a <p> element + +// First check that the element has child nodes +if (parg.hasChildNodes()) { + var children = parg.childNodes; + + for (var i = 0; i < children.length; i++) { + // do something with each child as children[i] + // NOTE: List is live, adding or removing children will change the list + } +}</pre> + +<hr> +<pre class="brush:js">// This is one way to remove all children from a node +// box is an object reference to an element + +while (box.firstChild) { + //The list is LIVE so it will re-index each call + box.removeChild(box.firstChild); +}</pre> + +<h2 id="Notes" name="Notes">備註</h2> + +<p>The items in the collection of nodes are objects and not strings. To get data from node objects, use their properties (e.g. <code>elementNodeReference.childNodes[1].nodeName</code> to get the name, etc.).</p> + +<p>The <code>document</code> object itself has 2 children: the Doctype declaration and the root element, typically referred to as <code>documentElement</code>. (In (X)HTML documents this is the <code>HTML</code> element.)</p> + +<p><code>childNodes</code> includes all child nodes, including non-element nodes like text and comment nodes. To get a collection of only elements, use {{ domxref("ParentNode.children") }} instead.</p> + +<h2 id="Specification" name="Specification">規範</h2> + +<ul> + <li><a class="external" href="http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1451460987">W3C DOM 2 Core: childNodes</a></li> + <li><a class="external" href="http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-1451460987">W3C DOM 3 Core: childNodes</a></li> + <li><a class="external" href="http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-536297177">W3C DOM 3 NodeList interface</a></li> +</ul> + +<h2 id="See_also" name="See_also">參見</h2> + +<ul> + <li>{{ domxref("Node.firstChild") }}</li> + <li>{{ domxref("Node.lastChild") }}</li> + <li>{{ domxref("Node.nextSibling") }}</li> + <li>{{ domxref("Node.previousSibling") }}</li> + <li>{{ domxref("ParentNode.children") }}</li> +</ul> |
