DIV CSS 佈局教程網

DOM 導航屬性:children 與 firstChild、lastChild 的區別
編輯:JavaScript綜合知識     

瀏覽器的 DOM 裡整個頁面是一棵樹。children 代表對象的子節點數組,而 firstChild/lastChild 取首尾子節點。但它們並不等價——一個只數元素,一個連文本節點一起數。

children 示例

<html>
<body>
  <table id="tbl">
    <tbody>
      <tr><td>行1列1</td><td>行1列2</td></tr>
      <tr><td>行2列1</td><td>行2列2</td></tr>
    </tbody>
  </table>

  <input onclick="alert(document.all.tbl.children(0).children(0).innerHTML)"
         value="children(0)" type="button">
  <input onclick="alert(document.all.tbl.children(0).children(1).innerHTML)"
         value="children(1)" type="button">
  <input onclick="alert(document.all.tbl.children(0).children(1).children(0).innerHTML)"
         value="children(2)" type="button">

  <div id=test>
    <a></a>
    <table></table>
  </div>

  <script>
    alert(test.children[0].tagName);      // A
    alert(test.children[1].tagName);      // TABLE
  </script>
</body>
</html>

逐層拆解 document.all.tbl.children(0).children(0).innerHTML:

document.all.tbl.children(0) 定位到 table 的第一個子節點,也就是 <tbody>;

再 .children(0) 定位到 tbody 的第一個子節點 <tr>,輸出 <td>行1列1</td><td>行1列2</td>。

換成 .children(1) 則是第二行,輸出 <td>行2列1</td><td>行2列2</td>;再接一個 .children(0) 就下鑽到具體單元格,輸出 行2列2。

而 test.children[0].tagName 和 test.children[1].tagName 分別是 A 和 TABLE——可見 children 只收元素節點,標籤之間的空白和換行被完全忽略。

firstChild / lastChild 示例

<table id="tbl">
  <tbody>
    <tr id="tr1"><td>行1列1</td><td>行1列2</td></tr>
    <tr><td id="tr2">行2列1</td><td>行2列2</td></tr>
  </tbody>
</table>

<input onclick="alert(document.all.tbl.firstChild.firstChild.innerHTML)"      value="first/first" type="button">
<input onclick="alert(document.all.tbl.firstChild.lastChild.innerHTML)"       value="first/last"  type="button">
<input onclick="alert(document.all.tbl.firstChild.lastChild.firstChild.innerHTML)" value="three" type="button">

<div id=test>
  <a></a>
  <table></table>
</div>

<script>
  alert(test.firstChild.tagName);    // A
  alert(test.lastChild.tagName);     // TABLE
</script>

解析:

document.all.tbl.firstChild 定位到 table 的第一個子節點 <tbody>;

再 .firstChild 定位到 tbody 的第一個子節點 <tr id="tr1">,輸出 <td>行1列1</td><td>行1列2</td>;

document.all.tbl.firstChild.lastChild 定位到 tbody 的最後一個子節點 <tr>,輸出 <td id="tr2">行2列1</td><td>行2列2</td>;

再接一個 .firstChild 下鑽到單元格,輸出 行2列2。

注意示例裡刻意把 <div id=test> 內部的標籤寫緊湊(<a></a> 緊接 <table>),所以 firstChild 拿到 A、lastChild 拿到 TABLE。

核心差異與實戰選擇

children 是 HTMLCollection,只包含元素節點(nodeType 1);firstChild/lastChild 是 Node 屬性,包含一切節點類型——元素、文本、註釋全都算。

這意味著:只要 HTML 裡標籤之間有換行或空格(幾乎必然有),firstChild 拿到的很可能是一個「什麼都不是」的文本節點,firstChild.tagName 返回 undefined。這是無數 DOM 腳本的 bug 之源。

解決方案:用專門的元素導航屬性。它們的命名規則是在中間插入 Element:

el.children            // 只含元素的集合
el.firstElementChild   // 第一個元素子節點
el.lastElementChild    // 最後一個元素子節點
el.nextElementSibling  // 下一個元素兄弟
el.previousElementSibling
el.parentElement       // 父元素

這些屬性從 IE9 起就被廣泛支持,語義明確、不受排版影響,新代碼應一律使用它們,把 firstChild/lastChild/nextSibling 這一族留給確實需要操作文本節點的場景。

另外,示例裡的 document.all.tbl 和 test(依賴 id 自動成為全局變量)都是老式寫法。標準做法是 document.getElementById("tbl");需要 CSS 選擇器能力時用 querySelector。而 children(0) 這種帶括號的調用形式也是 IE 特有,標準寫法是 children[0]。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved