先來(lái)看一張簡(jiǎn)單的文檔樹(shù)

很明顯樹(shù)的頂層節(jié)點(diǎn)是NodeA節(jié)點(diǎn),接下來(lái)可以通過(guò)指定的合適節(jié)點(diǎn)移動(dòng)到樹(shù)中的任何點(diǎn),結(jié)合以下的代碼你可以更好的了解這棵樹(shù)節(jié)點(diǎn)間的相互關(guān)系:
NodeA.firstChild = NodeA1
NodeA.lastChild = NodeA3
NodeA.childNodes.length = 3
NodeA.childNodes[0] = NodeA1
NodeA.childNodes[1] = NodeA2
NodeA.childNodes[2] = NodeA3
NodeA1.parentNode = NodeA
NodeA1.nextSibling = NodeA2
NodeA3.prevSibling = NodeA2
NodeA3.nextSibling = null
NodeA.lastChild.firstChild = NodeA3a
NodeA3b.parentNode.parentNode = NodeA
DOM定義對(duì)操作一個(gè)文檔對(duì)象的節(jié)點(diǎn)結(jié)構(gòu)提供了實(shí)用的方法,它提供了像執(zhí)行對(duì)象插入,更新,刪除,克隆等這些常用的方法。
insertBefore()--在參考子節(jié)點(diǎn)之前插入一個(gè)新的子節(jié)點(diǎn).如果參考的子節(jié)點(diǎn)為null,則新的子節(jié)點(diǎn)將作為調(diào)用節(jié)點(diǎn)的最后一個(gè)子節(jié)點(diǎn)插入。
replaceChild()--在childNodes集合種使用指定的newChild來(lái)代替oldChild;如果代替成功,則返回oldChild;如果newChild是null,則只需刪除oldChild即可。
removeChild()--從節(jié)點(diǎn)的ChildNodes集合中刪除removeChild指定的節(jié)點(diǎn),如果刪除成功,則返回刪除的子節(jié)點(diǎn)。
appendChild()--添加一個(gè)新節(jié)點(diǎn)到childNodes集合的末尾,如果成功,則返回新節(jié)點(diǎn)。
cloneNode()--創(chuàng)建一個(gè)新的、復(fù)制的節(jié)點(diǎn),并且如果傳入的參數(shù)是true時(shí),還將復(fù)制子節(jié)點(diǎn),如果節(jié)點(diǎn)是一個(gè)元素,那么還將復(fù)制相應(yīng)屬性,返回新的節(jié)點(diǎn)。
為了在一棵文檔樹(shù)中訪問(wèn)或者建立一個(gè)新的節(jié)點(diǎn),可以用下面這些方法:
getElementById()
getElementsByTagName()
createElement()
createAttribute()
createTextNode()
注意:在一個(gè)頁(yè)面中只有一個(gè)文檔對(duì)象,除了getElementsByTagName()外,其它方法均只能通過(guò)document.methodName()調(diào)用。
再看一下下面這個(gè)例子:
<html>
<head>
<title></title>
</head>
<body>
<p>This is a sample paragraph.</p>
<SCRIPT LANGUAGE="JavaScript">
<!--
alert(document.documentElement.lastChild.firstChild.tagName);
//-->
</SCRIPT>
</body>
</html>
結(jié)果將會(huì)顯示"P",下面是一些解釋
document.documentElement - gives the page's HTML tag.
lastChild - gives the BODY tag.
firstChild - gives the first element in the BODY.
tagName - gives that element's tag name, "P" in this case.
另一個(gè):
<html>
<head>
<title></title>
</head>
<body>
<p>This is a sample paragraph.</p>
<SCRIPT LANGUAGE="JavaScript">
<!--
alert(document.documentElement.lastChild.firstChild.tagName);
//-->
</SCRIPT>
</body>
</html>
這個(gè)例子和上面并沒(méi)有什么大的區(qū)別,僅僅是多了一個(gè)空行,但是在NS中,會(huì)自動(dòng)為空行加上一個(gè)節(jié)點(diǎn)所以返回值是"undefined",而在IE中將跳過(guò)空行仍然指向P標(biāo)簽。
更常用的方法:
<p id="myParagraph">This is a sample paragraph.</p>
...
alert(document.getElementById("myParagraph").tagName);
這種方法你不用關(guān)心節(jié)點(diǎn)在文檔樹(shù)的哪一個(gè)地方,而只要保證在頁(yè)面中它的ID號(hào)是唯一的就可以了。
接下來(lái)一種訪問(wèn)元素節(jié)點(diǎn)的方法是document.getElementsByTagName(),它的返回值是一個(gè)數(shù)組,例如你可以通過(guò)下面的例子改變整個(gè)頁(yè)面的連接:
var nodeList = document.getElementsByTagName("A");
for (var i = 0; i < nodeList.length; i++)
nodeList[i].style.color = "#ff0000";
attribute和attributes
attribute對(duì)象和元素相關(guān),但是卻沒(méi)有被認(rèn)為是文檔樹(shù)的一部分,因此屬性不能作為子節(jié)點(diǎn)集合的一部分來(lái)使用。
有三種方法可以為元素建立新的屬性
1.
var attr = document.createAttribute("myAttribute");
attr.value = "myValue";
var el = document.getElementById("myParagraph");
el.setAttributeNode(attr);
2.
var el = document.getElementById("myParagraph");
el.setAttribute("myAttribute", "myValue");
3.
var el = document.getElementById("myParagraph");
el.myAttribute = "myValue";
你可以在html標(biāo)簽種定義自己的屬性:
<p id="myParagraph" myAttribute="myValue">This is a sample paragraph.</p>
...
alert(document.getElementById("myParagraph").getAttribute("myAttribute"));
返回值將是"myValue".但是請(qǐng)注意這里必須使用getAttribute,而不是AttributeName,因?yàn)橛幸恍g覽器并不支持自定義屬性。
attributes也可以被輕易的從一個(gè)元素中刪除,你可以使用removeAttribute()或者將element.attributeName指向一個(gè)null值。
通過(guò)attributes我們就可以產(chǎn)生一些動(dòng)態(tài)效果:
<p id="sample1" align="left">Text in a paragraph element.</p>
... code for the links ...
document.getElementById('sample1').setAttribute('align', 'left');
document.getElementById('sample1').setAttribute('align', 'right');
另一種:
<p id="sample2" style="text-align:left;">Text in a paragraph
element.</p>
... code for the links ...
document.getElementById('sample2').style.textAlign = 'left';
document.getElementById('sample2').style.textAlign = 'right';
跟上面的例子一樣,展示了可用通過(guò)元素修改style中的屬性,甚至是class中的.唯一要提到的是textAlign是從style中的text-align中演變而來(lái)的,有一條基本規(guī)律,就是style中的屬性如果出現(xiàn)-則在dom中將會(huì)被去掉并且隨后的一個(gè)字母將改為大寫(xiě),還有一點(diǎn)就是如果即使元素中沒(méi)有style屬性,上述例子同樣可以使用。
text nodes:
先看一下例子:
<p id="sample1">This is the initial text.</p>
... code for the links ...
document.getElementById('sample1').firstChild.nodeValue =
'Once upon a time...';
document.getElementById('sample1').firstChild.nodeValue =
'...in a galaxy far, far away';
首先text nodes并沒(méi)有像elements那樣具有id屬性,所有它并不能直接通過(guò)document.getElementById()或者document.getElementsByTagName()訪問(wèn)
看一下下面的結(jié)構(gòu)也許會(huì)更明白一些:

可以看出通過(guò)document.getElementById('sample1').firstChild.nodeValue就可以讀取或者設(shè)置text nodes的值了。
另一個(gè)更加復(fù)雜一點(diǎn)的例子:
<p id="sample2">This is the <b>initial</b> text.</p>
它的文檔結(jié)構(gòu)

在這里通過(guò)document.getElementById('sample1').firstChild.nodeValue講僅僅改變"This is the"
而initial text.將不會(huì)改變.在這里大家應(yīng)該看到了它和innerHTML的不同了.當(dāng)然你也可以這樣用:
document.getElementById('sample3').firstChild.nodeValue =
'<b>Once</b> upon a time...';
document.getElementById('sample3').firstChild.nodeValue =
'...in a galaxy <i>far, far</i> away';
其中的html代碼將不會(huì)被解釋,瀏覽器將把他們當(dāng)成普通的文本來(lái)顯示。
創(chuàng)建和刪除text nodes:
var myTextNode = document.createTextNode("my text");
通過(guò)上面的代碼你可以創(chuàng)建一個(gè)新的text node,但是它并不是文檔樹(shù)的一部分,要讓它顯示在頁(yè)面上就必須讓它成為文檔樹(shù)中某一個(gè)節(jié)點(diǎn)的child,因?yàn)?br />
text nodes不能有兒子,所以你不能將它加入到一個(gè)text nodes中,attribute也不屬于文檔樹(shù)的一部分,這條路也不行,現(xiàn)在只剩下elements nodes
了,以下的例子展示了如何添加和刪除一個(gè)text node:
<p id="sample1">Initial text within a paragraph element.</p>
... code to add a text node ...
var text = document.createTextNode(" new text " + (++counter1));
var el = document.getElementById("sample1");
el.appendChild(text);
... code to remove the last child node ...
var el = document.getElementById("sample1");
if (el.hasChildNodes())
el.removeChild(el.lastChild);
增加文本是很容易的,上面的代碼建立了一個(gè)新的text node并且通過(guò)appendChild()方法將其加入到childNodes數(shù)組的末尾,并設(shè)置了一個(gè)counter1的全局變量,利于觀察
hasChildNodes()的返回值是true or false;用來(lái)判斷當(dāng)前節(jié)點(diǎn)是否還有child,以阻止當(dāng)其沒(méi)有child的時(shí)候調(diào)用removeChild()產(chǎn)生的錯(cuò)誤。
創(chuàng)建element nodes
有了上面的基礎(chǔ),應(yīng)該更容易理解了,先看一下下面的代碼
<div id="sample1">This text is in a DIV element.</div>
... code for the link ...
var paraEl, boldEl;
paraEl = document.createElement("p");
boldEl = document.createElement("b");
paraEl.appendChild(document.createTextNode("This is a new paragraph with "));
boldEl.appendChild(document.createTextNode("bold"));
paraEl.appendChild(boldEl);
paraEl.appendChild(document.createTextNode(" text added to the DIV"));
document.getElementById("sample1").appendChild(paraEl);
你還可以直接為新加的element nodes設(shè)置attribute,以下兩種都可以:
boldEl.style.color = "#ffff00";
paraEl.appendChild(boldEl);
或者:
paraEl.appendChild(boldEl);
boldEl.style.color = "#ffff00";
|