DOM定义Ҏ作一个文档对象的节点l构提供了实用的Ҏ,它提供了像执行对象插?更新,删除,克隆{这些常用的Ҏ?br />insertBefore()--在参考子节点之前插入一个新的子节点.如果参考的子节点ؓnull,则新的子节点作用节点的最后一个子节点插入?br />replaceChild()--在childNodes集合U用指定的newChild来代替oldChild;如果代替成功,则返回oldChild;如果newChild是null,则只需删除oldChild卛_?br />removeChild()--从节点的ChildNodes集合中删除removeChild指定的节?如果删除成功,则返回删除的子节炏V?br />appendChild()--d一个新节点到childNodes集合的末?如果成功,则返回新节点?br />cloneNode()--创徏一个新的、复制的节点,q且如果传入的参数是true?q将复制子节?如果节点是一个元?那么q将复制相应属?q回新的节点?br /> Z在一|树中访问或者徏立一个新的节?可以用下面这些方? getElementById() getElementsByTagName() createElement() createAttribute() createTextNode() 注意:在一个页面中只有一个文对?除了getElementsByTagName()?其它Ҏ均只能通过document.methodName()调用?br /> 再看一下下面这个例? <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> l果会昄"P",下面是一些解?br />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. 另一? <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> q个例子和上面ƈ没有什么大的区?仅仅是多了一个空?但是在NS?会自动ؓI加上一个节Ҏ以返回值是"undefined",而在IE中将跌I仍然指向P标签?br /> 更常用的Ҏ: <p id="myParagraph">This is a sample paragraph.</p> ... alert(document.getElementById("myParagraph").tagName); q种Ҏ你不用关心节点在文档树的哪一个地?而只要保证在面中它的IDh唯一的就可以了?br /> 接下来一U访问元素节点的Ҏ是document.getElementsByTagName(),它的q回值是一个数l?例如你可以通过下面的例子改变整个页面的q接Q?br />var nodeList = document.getElementsByTagName("A"); for (var i = 0; i < nodeList.length; i++) nodeList[i].style.color = "#ff0000";
attribute和attributes attribute对象和元素相?但是却没有被认ؓ是文树的一部分,因此属性不能作为子节点集合的一部分来用?br />有三U方法可以ؓ元素建立新的属?br />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标签U定义自q属? <p id="myParagraph" myAttribute="myValue">This is a sample paragraph.</p> ... alert(document.getElementById("myParagraph").getAttribute("myAttribute")); q回值将?myValue".但是h意这里必M用getAttribute,而不是AttributeName,因ؓ有一些浏览器q不支持自定义属性?br /> attributes也可以被L的从一个元素中删除,你可以用removeAttribute()或者将element.attributeName指向一个null倹{?br />通过attributes我们可以生一些动态效果: <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'); 另一U? <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'; 跟上面的例子一?展示了可用通过元素修改style中的属?甚至是class中的.唯一要提到的是textAlign是从style中的text-align中演变而来?有一条基本规?是style中的属性如果出?则在dom中将会被Lq且随后的一个字母将改ؓ大写,q有一点就是如果即使元素中没有style属?上述例子同样可以使用?br /> 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 nodesq没有像elements那样hid属?所有它q不能直接通过document.getElementById()或者document.getElementsByTagName()讉K 看一下下面的l构也许会更明白一些: 可以看出通过document.getElementById('sample1').firstChild.nodeValue可以读取或者设|text nodes的g?br /> 另一个更加复杂一点的例子Q?br /><p id="sample2">This is the <b>initial</b> text.</p> 它的文l构 在这里通过document.getElementById('sample1').firstChild.nodeValue讲仅仅改?This is the" ?b>initial text.不会改?在这里大家应该看C它和innerHTML的不同了.当然你也可以q样用: 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代码不会被解释,览器将把他们当成普通的文本来显C?br /> 创徏和删除text nodes: var myTextNode = document.createTextNode("my text"); 通过上面的代码你可以创徏一个新的text node,但是它ƈ不是文树的一部分,要让它显C在面上就必须让它成ؓ文树中某一个节点的child,因ؓ text nodes不能有儿?所以你不能它加入C个text nodes?attribute也不属于文树的一部分,q条路也不行,现在只剩下elements nodes ?以下的例子展CZ如何d和删除一个text nodeQ?br /><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); 增加文本是很Ҏ?上面的代码徏立了一个新的text nodeq且通过appendChild()Ҏ其加入到childNodes数组的末?q设|了一个counter1的全局变量,利于观察 hasChildNodes()的返回值是true or false;用来判断当前节点是否q有child,以阻止当其没有child的时候调用removeChild()产生的错误?br /> 创徏element nodes 有了上面的基,应该更容易理解了,先看一下下面的代码 <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讄attribute,以下两种都可? boldEl.style.color = "#ffff00"; paraEl.appendChild(boldEl); 或? paraEl.appendChild(boldEl); boldEl.style.color = "#ffff00";