標籤:
一、層次節點的概述
節點的階層可以劃分為:父節點與子節點、兄弟節點這兩種。當我們擷取其中一個元素節點的時候,就可以使用層次節點屬性來擷取它相關層次的節點。
二、childNodes 屬性
childeNodes 屬性可以擷取某一個元素節點的所有子節點,這些子節點包含元素子節點和文本子節點。
<script type="text/javascript"> window.onload = function(){ var box = document.getElementById(‘box‘); alert(box.childNodes);//[object NodeList]返回的是一個數組,數組裡面是當前元素節點所有的子節點列表 alert(box.childNodes.length); //3 擷取子節點的個數 //3個子節點為:測試Div<em>傾斜</em>結尾 //第一個子節點為:測試Div,稱為文本節點 //第二個子節點為:<em>傾斜</em>,稱為元素節點 //第三個子節點為:結尾 ,稱為文本節點 alert(box.childNodes[0]); //結果是:[object Text]表示一個文本節點對象 alert(box.childNodes[0].nodeType); //結果是:3 表示文本節點 alert(box.childNodes[0].nodeValue); //擷取文本節點的常值內容 alert(box.childNodes[0].innerHTML); //當前本身就是文本,不能再擷取裡面的文本所以是undefined alert(box.childNodes[0].nodeName);// #text 文本節點沒有標籤名 }</script></head><body> <div id="box" class="pox" title="標題" style="color:#F00;" bbb="aaa">測試Div<em>傾斜</em>結尾</div></body>
通過判斷節點類型來擷取不同的輸出
<script type="text/javascript"> window.onload = function () { var box = document.getElementById(‘box‘); for (var i = 0; i < box.childNodes.length; i ++) { if (box.childNodes[i].nodeType === 1) { //判斷是元素節點,輸出元素標籤名 alert(‘元素節點:‘ + box.childNodes[i].nodeName); } else if (box.childNodes[i].nodeType === 3) { //判斷是文本節點,輸出常值內容 alert(‘文本節點:‘ + box.childNodes[i].nodeValue); } } };</script></head><body> <div id="box" class="pox" title="標題" style="color:#F00;" bbb="aaa">測試Div<em>傾斜</em>結尾</div></body>
在擷取到文本節點的時候,是無法使用 innerHTML 這個屬性輸出常值內容的。這個非標準的屬性必須在擷取元素節點的時候,才能輸出裡麵包含的文本
<script type="text/javascript"> window.onload = function () { var pox = document.getElementById(‘pox‘); alert(pox); //pox.innerHTML=‘測試<strong>Pox</strong>‘; pox.nodeValue = ‘測試Pox‘;//沒有報錯,但沒有賦值成功 //pox.childNodes[0].nodeValue = ‘測試Pox‘; pox.childNodes[0].nodeValue = ‘測試<strong>Pox</strong>‘; }</script></head><body> <div id="box" title="標題" style="color:#F00;" bbb="aaa">測試Div<em>傾斜</em>結尾</div> <div id="pox">1</div></body>
三、firstChild 和 lastChild 屬性
firstChild 用於擷取當前元素節點的第一個子節點,相當於 childNodes[0]
lastChild 用於擷取當前元素節點的最後一個子節點,相當於 childNodes[box.childNodes.length - 1]
<script type="text/javascript"> window.onload = function () { var box = document.getElementById(‘box‘); alert(box.childNodes[0].nodeValue); //擷取第一個子節點 alert(box.childNodes[box.childNodes.length-1].nodeValue);//擷取最後一個子節點 //上面的方法比較煩可以使用下面的額方法 alert(box.firstChild.nodeValue);//擷取第一個子節點 alert(box.lastChild.nodeValue);//擷取最後一個子節點 }</script></head><body> <div id="box" class="pox" title="標題" style="color:#F00;" bbb="aaa">測試Div<em>傾斜</em>結尾</div> <div id="pox">1</div></body>
四、ownerDocument 屬性
ownerDocument 屬性返回該節點的文檔對象根節點,返回的對象相當於 document。
<script type="text/javascript"> window.onload = function () { var box = document.getElementById(‘box‘); alert(box.ownerDocument); //[object HTMLDocument]//跟節點 文檔對象 alert(box.document); //同上 alert(box.ownerDocument === document); //true,根節點 alert(box.ownerDocument.nodeName); //#document alert(box.ownerDocument.nodeType); //類型返回的是9 }</script></head><body> <div id="box" class="pox" title="標題" style="color:#F00;" bbb="aaa">測試Div<em>傾斜</em>結尾</div> <div id="pox">1</div></body>
五、parentNode、previousSibling、nextSibling 屬性
parentNode 屬性返回該節點的父節點
previousSibling 屬性返回該節點的前一個同級節點
nextSibling 屬性返回該節點的後一個同級節點。
<script type="text/javascript"> window.onload = function () { var box = document.getElementById(‘box‘); alert(box.parentNode.nodeName); //擷取父節點的標籤名 結果是body alert(box.lastChild.previousSibling); //擷取前一個同級節點即最後一個節點(也就是(結尾)的上個節點(<em>傾斜</em>) 是一個元素節點) alert(box.firstChild.nextSibling); //擷取後一個同級節點 ((即測試Div)這個節點的下個節點 (<em>傾斜</em>) 是一個元素節點) alert(box.firstChild.nextSibling.nodeName);//結果是em }</script></head><body> <div id="box" class="pox" title="標題" style="color:#F00;" bbb="aaa">測試Div<em>傾斜</em>結尾</div></body>
六、attributes 屬性
attributes 屬性返回該節點的屬性節點集合。
<script type="text/javascript"> window.onload = function () { var box = document.getElementById(‘box‘); alert(box.attributes); //NamedNodeMap 也是個集合數組 集合中儲存著屬性節點列表 alert(box.attributes.length); //屬性節點的個數 alert(box.attributes[0]); //[object Attr]最後一個屬性節點對象 0代表的是最後一個是從後往前搜尋的 alert(box.attributes[0].nodeType); //2,節點類型 alert(box.attributes[0].nodeValue); //最後一個屬性的屬性值 alert(box.attributes[0].nodeName); //最後一個屬性的屬性名稱 alert(box.attributes[‘title‘]); //[object Attr],返回屬性為 title 的節點 alert(box.attributes[‘title‘].nodeValue); alert(box.attributes.getNamedItem(‘title‘)); //同上 }</script></head><body> <div id="box" class="pox" title="標題" style="color:#F00;" bbb="aaa">測試Div<em>傾斜</em>結尾</div></body>
JavaScript的DOM_擷取和操作層次節點