JS中Node節點總結

來源:互聯網
上載者:User

標籤:eva   節點   one   rtb   元素   名稱   replace   char   move   

Node的三個基本屬性:

  1.nodeType:表明節點類型,1是元素節點,3是文本節點。

  2.nodeName:  表明節點名稱,元素節點為標籤名,文本節點為#text。

  3.nodeValue:表明節點值,元素節點為null,文本節點為常值內容。

Node的節點關係:

  1.parentNode與childNodes

    parentNode尋找當前節點的父節點,childNodes尋找當前節點的子節點,其中包括文本節點和元素節點(特別需要注意空白的文本節點也算是節點)如下程式

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>childNodes.length</title></head><body>    <ul id="ul">        <li></li>        <li></li>        <li></li>    </ul>    <script>        var oUl=document.getElementById(‘ul‘);        alert(oUl.childNodes.length);//是7不是3    </script></body></html>

  2:firstChild與lastChild

    分別對應父節點的第一個節點與最後一個節點,firstChild相當於childNodes[0]或childNodes.item(0)。

  3.nextSibling與previousSibling

    分別對應當前節點的前一個節點與當前元素的後一個節點。

Node的動作節點:

  Node的動作節點有appendChild(),insertBefore(),replaceChild(),removeChild(),cloneNode(),normalize()

  1.appendChild(newNode)是將newNode添加到當前節點的最後一個子節點的後面,如果newNode已經是文檔的一部分,則appendChild操作相當於剪下;若果newNode是新創造的節點,則appendChild操作相當於添加。

  2.insertBefore(newNode,oldNode)是將newNode添加到當前節點的oldNode子節點的前面,如果insertBefore(newNode,null),則insertBefore()的操作和appendChild()一樣。

  3.replaceChild(newNode,oldNode)是將oldNode替換為newNode,不過oldNode的所有關係指標newNode也都繼承下來。

  4.removeChild(oldNode)是刪除oldNode節點。

  5.cloneNode(true/false)是將已知節點複製一份,如果為true,包括子節點,若果為false,不包括子節點。然後通過appendChild(),insertBefore(),replaceChild()將其添加到文檔中。

  6.normalize()是將空文本節點刪除或將相鄰的文本節點合并為一個文本節點。

JS中Node節點總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.