- 浏览: 42585 次
- 性别:
- 来自: Beijing
文章分类
最新评论
-
xwsoft:
Spring中文开发手册下载 -
zhengbin917943:
谢了~~
Spring中文开发手册下载 -
steed_ns:
谢了分享~~~~~~~
Spring中文开发手册下载 -
xms_999:
谢谢分享
使用DOM技术操纵文档 -
samuelyuan:
垃圾,严重鄙视抄袭!
Guice 1.0 手册 (附带一类似HelloWorld例子)
java 代码
- [资料]JS使用DOM技术操纵文档 -- 创亿无限|创意无限|创意| -- 编程爱好者BLOG
- JavaScript高级应用:使用DOM技术操纵文档
- 我们知道,如果使用DHTML对象模型对文档的内容进行操纵,就必须了解相关文档元素的特殊属性和方法。因为文档的元素有很多种,相应的属性和方法也就非常多,为了实现一个功能,开发人员不得不查询许多资料。但是有了DOM技术,一切变得简单起来,那些操纵文档内容的属性和方法具备了元素无关性(element-independent)特点,这使得我们能够按照相同的步骤操纵文档内容。毋庸置疑,DOM技术为开发人员在对象层次上操纵文档提供了最便捷的手段。
- 本文就为你全面介绍在JavaScript中如何使用DOM属性和方法操纵文档内容。
- 使用data、nodeValue和src属性
- DOM提供了2个属性用于修改文本节点的内容,它们是data和nodeVaule。2个属性实现的功能相同,语法是:object.data="new
- value"或者object.nodeVaule="new
- value",其中object代表页面中的文本项节点。如果修改图形文件的内容,语法是:object.src=../../"new
- value",其中object表示页面中的img标记节点。来看看下面的例子:
- <HTML><HEAD><TITLE> DOM Demo </title></HEAD>
- <BODY ID="bodyNode">
- This is the document body
- <P ID = "p1Node">This is paragraph 1.</P>
- <P ID = "p2Node">This is paragraph 2.</P>
- <P ID = "p3Node">This is paragraph 3.
- <IMG ID = "imgNode" SRC="myexam.gif">This text follows the image
- <TABLE ID="tableNode">
- <TR><TD BGCOLOR=yellow>This is row 1, cell 1</TD><TD
- BGCOLOR=orange>This is row 1, cell 2</TD></TR>
- <TR><TD BGCOLOR=red>This is row 2, cell 1</TD><TD BGCOLOR=magenta>This
- is row 2, cell 2</TD></TR>
- <TR><TD BGCOLOR=lightgreen>This is row 3, cell 1</TD><TD
- BGCOLOR=beige>This is row 3, cell 2</TD></TR>
- </TABLE>
- </P>
- <P ID = "p4Node">This is paragraph 4.</P>
- <SCRIPT LANGUAGE="JavaScript">
- <!--
- alert(
- "页面初始状态" + "\n\n" +
- "bodyNode.firstChild.nodeValue = " + bodyNode.firstChild.nodeValue + "\n" +
- "bodyNode.firstChild.data = " + bodyNode.firstChild.data + "\n"
- );
- bodyNode.firstChild.nodeValue = "This is the new document body, set by
- the nodeValue property";
- p3Node.childNodes[1].src = "myexam2.gif";
- alert(
- "对页面内容进行修改后" + "\n\n" +
- "bodyNode.firstChild.nodeValue = " + bodyNode.firstChild.nodeValue + "\n" +
- "bodyNode.firstChild.data = " + bodyNode.firstChild.data + "\n" +
- "p3Node.childNodes[1].src = " + p3Node.childNodes[1].src
- );
- // -->
- </SCRIPT></BODY></HTML>
- 上述代码首先显示页面的初始内容以及节点p1Node的相关属性值,然后对节点bodyNode的第1个孩子节点的nodeValue属性进行赋值修改其文本内容,对节点p3Node的第2个孩子节点(也就是了img标记)的src属性进行赋值修改图形的内容。点击这里运行这个实例。
- JavaScript高级应用:使用DOM技术操纵文档
- 使用createElement方法
- createElement方法的功能是在文档中创建一个孤立HTML标记节点。孤立节点既没有孩子节点也没有双亲节点,而且与文档中的现存节点也互不关联。被创建的孤立HTML标记节点的唯一信息就是它代表的HTML标记,比如<
- P>、< FONT>和< TABLE>。CreateElement的语法为document.createElement(HTMLTag),HTMLTag表示要创建的HTML标记,返回值是创建的孤立HTML标记节点。来看看下面的例子:
- <HTML><HEAD><TITLE> DOM Demo </title></HEAD>
- <BODY ID="bodyNode">
- <SCRIPT LANGUAGE="JavaScript">
- <!--
- alert("页面初始状态");
- tableObj = document.createElement("TABLE");
- tbodyObj = document.createElement("TBODY");
- trObj = document.createElement("TR");
- tdObj = document.createElement("TD");
- alert(
- "对页面内容进行修改后" + "\n" +
- "bodyNode.firstChild = " + bodyNode.firstChild + "\n" +
- "tableObj.nodeName = " + tableObj.nodeName + "\n" +
- "tbodyObj.nodeName = " + tbodyObj.nodeName + "\n" +
- "trObj.nodeName = " + trObj.nodeName + "\n" +
- "tdObj.nodeName = " + tdObj.nodeName + "\n" +
- "tableObj = " + tableObj + "\n"
- );
- // -->
- </SCRIPT>
- </BODY>
- </HTML>
- 上述代码在文档中创建了4个孤立HTML标记节点tableObj、tbodyObj、trObj和tdObj,它们就象太空中4颗人造卫星一样,彼此间以及和文档中的现有节点间都互不关联。创建孤立节点的目的不是为了让它真正孤立,随后我们会介绍如何将创建的孤立节点彼此相连,并添加到文档的DOM
- Tree结构中。点击这里运行这个实例。
- 使用createTextNode方法
- createTextNode方法的功能与creatElement方法几乎相同,唯一不同的就是它是在文档中创建一个孤立文本项节点,这个孤立节点的唯一信息就是它所代表的字符串值。createTextNode的语法为document.
- createTextNode (string),string表示要创建的文本项内容,返回值是创建的孤立文本项节点。来看看下面的例子:
- <HTML><HEAD><TITLE> DOM Demo </title></HEAD>
- <BODY ID="bodyNode">
- <SCRIPT LANGUAGE="JavaScript">
- <!--
- alert("页面初始状态");
- row1cell1Obj = document.createTextNode("This is row 1, cell 1");
- row1cell2Obj = document.createTextNode("This is row 1, cell 2");
- row2cell1Obj = document.createTextNode("This is row 2, cell 1");
- row2cell2Obj = document.createTextNode("This is row 2, cell 2");
- row3cell1Obj = document.createTextNode("This is row 3, cell 1");
- row3cell2Obj = document.createTextNode("This is row 3, cell 2");
- alert(
- "对页面内容进行修改后" + "\n" +
- "row1cell1Obj.firstchild = " + row1cell1Obj.firstChild + "\n" +
- "row1cell1Obj.nodeName = " + row1cell1Obj
- );
- // -->
- </SCRIPT></BODY></HTML>
- 上述代码在文档中创建了6个孤立文本项节点,它们表示一个3行2列大小Table的单元格内容。点击这里运行这个实例。
- JavaScript高级应用:使用DOM技术操纵文档
- 使用cloneNode方法
- cloneNode方法的功能是通过克隆(也就是复制)文档中一个现存节点的方式创建文档的一个孤立节点。根据被克隆节点的类型,新创建的孤立节点或者是HTML标记节点,或者是包含字符串信息的文本项节点。cloneNode的语法为oldNode.createNode(false)或者oldNode.createNode(true),oldNode表示被克隆的节点,返回值是新创建的节点,false表示仅仅克隆oldNode,true表示克隆oldNode并包含其下属节点。来看看下面的例子:
- <HTML><HEAD><TITLE> DOM Demo </title></HEAD>
- <BODY ID="bodyNode">
- <SCRIPT LANGUAGE="JavaScript">
- <!--
- alert("页面初始状态");
- tableObj = document.createElement("TABLE");
- tbodyObj = document.createElement("TBODY");
- tr1Obj = document.createElement("TR");
- tr2Obj = tr1Obj.cloneNode(false);
- tr3Obj = tr1Obj.cloneNode(false);
- tr1td1Obj = document.createElement("TD");
- tr1td2Obj = tr1td1Obj.cloneNode(false);
- tr2td1Obj = tr1td1Obj.cloneNode(false);
- tr2td2Obj = tr1td1Obj.cloneNode(false);
- tr3td1Obj = tr1td1Obj.cloneNode(false);
- tr3td2Obj = tr1td1Obj.cloneNode(false);
- row1cell1Obj = document.createTextNode("This is row 1, cell 1");
- row1cell2Obj = row1cell1Obj.cloneNode(false);
- row2cell1Obj = row1cell1Obj.cloneNode(false);
- row2cell2Obj = row1cell1Obj.cloneNode(false);
- row3cell1Obj = row1cell1Obj.cloneNode(false);
- row3cell2Obj = row1cell1Obj.cloneNode(false);
- row1cell2Obj.nodeValue = "This is row 1, cell 2";
- row2cell1Obj.nodeValue = "This is row 2, cell 1";
- row2cell2Obj.nodeValue = "This is row 2, cell 2";
- row3cell1Obj.nodeValue = "This is row 3, cell 1";
- row3cell2Obj.nodeValue = "This is row 3, cell 2";
- alert(
- "对页面内容进行修改后" + "\n" +
- "row1cell2Obj.firstChild = " + row1cell2Obj.firstChild+ "\n" +
- "row1cell2Obj = " + row1cell2Obj + "\n" +
- "row3cell2Obj.nodeValue = " + row3cell2Obj.nodeValue + "\n" +
- "tr3td2Obj.nodeName = " + tr3td2Obj.nodeName + "\n"
- );
- // -->
- </SCRIPT></BODY></HTML>
- 上述代码中值得注意的是对文本项节点的处理方式:
- 1、首先,使用语句row1cell1Obj = document.createTextNode("This is row 1, cell
- 1")创建单元格(1,1)所在节点;
- 2、然后使用语句row1cell1Obj.cloneNode(false)分别创建其他单元格节点;
- 3、最后,使用语句node.nodeValue = string分别为不同单元格赋值。
- 点击这里运行这个实例。
- JavaScript高级应用:使用DOM技术操纵文档
- 使用appendChild方法
- appendChild方法的功能是在2个节点间建立起父子关系,如果作为父亲的节点已经具有了孩子节点,那么新添加的孩子节点被追加为最后一个孩子,也就是lastChild。appendChild的语法是fatherObj.appendChild(childObj),返回值是被追加的孩子节点。来看看下面的例子:
- <HTML><HEAD><TITLE> DOM Demo </title></HEAD>
- <BODY ID="bodyNode">
- <SCRIPT LANGUAGE="JavaScript">
- <!--
- alert("页面初始状态");
- tableObj = document.createElement("TABLE");
- tbodyObj = document.createElement("TBODY");
- tr1Obj = document.createElement("TR");
- tr2Obj = tr1Obj.cloneNode();
- tr3Obj = tr1Obj.cloneNode();
- tr1td1Obj = document.createElement("TD");
- tr1td2Obj = tr1td1Obj.cloneNode();
- tr2td1Obj = tr1td1Obj.cloneNode();
- tr2td2Obj = tr1td1Obj.cloneNode();
- tr3td1Obj = tr1td1Obj.cloneNode();
- tr3td2Obj = tr1td1Obj.cloneNode();
- row1cell1Obj = document.createTextNode("This is row 1, cell 1");
- row1cell2Obj = row1cell1Obj.cloneNode();
- row2cell1Obj = row1cell1Obj.cloneNode();
- row2cell2Obj = row1cell1Obj.cloneNode();
- row3cell1Obj = row1cell1Obj.cloneNode();
- row3cell2Obj = row1cell1Obj.cloneNode();
- row1cell2Obj.nodeValue = "This is row 1, cell 2";
- row2cell1Obj.nodeValue = "This is row 2, cell 1";
- row2cell2Obj.nodeValue = "This is row 2, cell 2";
- row3cell1Obj.nodeValue = "This is row 3, cell 1";
- row3cell2Obj.nodeValue = "This is row 3, cell 2";
- returnValue = tableObj.appendChild(tbodyObj);
- tbodyObj.appendChild(tr1Obj);
- tbodyObj.appendChild(tr2Obj);
- tbodyObj.appendChild(tr3Obj);
- tr1Obj.appendChild(tr1td1Obj);
- tr1Obj.appendChild(tr1td2Obj);
- tr2Obj.appendChild(tr2td1Obj);
- tr2Obj.appendChild(tr2td2Obj);
- tr3Obj.appendChild(tr3td1Obj);
- tr3Obj.appendChild(tr3td2Obj);
- tr1td1Obj.appendChild(row1cell1Obj);
- tr1td2Obj.appendChild(row1cell2Obj);
- tr2td1Obj.appendChild(row2cell1Obj);
- tr2td2Obj.appendChild(row2cell2Obj);
- tr3td1Obj.appendChild(row3cell1Obj);
- tr3td2Obj.appendChild(row3cell2Obj);
- bodyNode.appendChild(tableObj);
- alert("对页面内容进行修改后");
- // -->
- </SCRIPT></BODY></HTML>
- 上面的代码演示了一个Table的动态创建过程。首先创建Table的各个行、列以及单元格的节点,然后使用appendChild将这些节点进行连接形成一个Table,最后通过语句bodyNode.appendChild(tableObj)将Table装载进文档中。点击这里运行这个实例
- JavaScript高级应用:使用DOM技术操纵文档
- 使用applyElement方法
- applyElement方法的功能是将一个节点与它的孩子节点和父亲节点脱离,然后将另外一个节点连接到这个节点,最终使它们成为父子关系。applyElement的语法是childObj.applyElement(fatherObj),返回值是被连接的孩子节点。
- 我们注意到,applyElement和appendChild实现的目的基本相同,都是在2个节点间建立父子关系,但有2个区别:
- 1、applyElement方法只能操纵HTML标记节点,不能处理文本项节点。appendNode则能处理2种节点。
- 2、appendNode对2个节点的连接方式是从父到子,applyElement则是从子到父。
- 来看看下面的例子:
- <HTML><HEAD><TITLE> DOM Demo </title></HEAD>
- <BODY ID="bodyNode">
- <SCRIPT LANGUAGE="JavaScript">
- <!--
- alert("页面初始状态");
- tableObj = document.createElement("TABLE");
- tbodyObj = document.createElement("TBODY");
- tr1Obj = document.createElement("TR");
- tr2Obj = tr1Obj.cloneNode();
- tr3Obj = tr1Obj.cloneNode();
- tr1td1Obj = document.createElement("TD");
- tr1td2Obj = tr1td1Obj.cloneNode();
- tr2td1Obj = tr1td1Obj.cloneNode();
- tr2td2Obj = tr1td1Obj.cloneNode();
- tr3td1Obj = tr1td1Obj.cloneNode();
- tr3td2Obj = tr1td1Obj.cloneNode();
- row1cell1Obj = document.createTextNode("This is row 1, cell 1");
- row1cell2Obj = row1cell1Obj.cloneNode();
- row2cell1Obj = row1cell1Obj.cloneNode();
- row2cell2Obj = row1cell1Obj.cloneNode();
- row3cell1Obj = row1cell1Obj.cloneNode();
- row3cell2Obj = row1cell1Obj.cloneNode();
- row1cell2Obj.nodeValue = "This is row 1, cell 2";
- row2cell1Obj.nodeValue = "This is row 2, cell 1";
- row2cell2Obj.nodeValue = "This is row 2, cell 2";
- row3cell1Obj.nodeValue = "This is row 3, cell 1";
- row3cell2Obj.nodeValue = "This is row 3, cell 2";
- tr1td1Obj.appendChild(row1cell1Obj);
- tr1td2Obj.appendChild(row1cell2Obj);
- tr2td1Obj.appendChild(row2cell1Obj);
- tr2td2Obj.appendChild(row2cell2Obj);
- tr3td1Obj.appendChild(row3cell1Obj);
- tr3td2Obj.appendChild(row3cell2Obj);
- tr1td1Obj.applyElement(tr1Obj);
- tr1Obj.appendChild(tr1td2Obj);
- tr2td1Obj.applyElement(tr2Obj);
- tr2Obj.appendChild(tr2td2Obj);
- tr3td1Obj.applyElement(tr3Obj);
- tr3Obj.appendChild(tr3td2Obj);
- tr1Obj.applyElement(tbodyObj);
- tbodyObj.appendChild(tr2Obj);
- tbodyObj.appendChild(tr3Obj);
- returnValue = tbodyObj.applyElement(tableObj);
- bodyNode.appendChild(tableObj);
- alert("对页面内容进行修改后");
- // -->
- </SCRIPT></BODY></HTML>
- 上面的代码同样演示了一个Table的动态创建过程。首先创建Table的各个行、列以及单元格的节点,然后混合使用applyElement和appendChild将这些节点进行连接形成一个Table,最后通过语句bodyNode.appendChild(tableObj)将Table装载进文档中。点击这里运行这个实例。
- JavaScript高级应用:使用DOM技术操纵文档
- 使用insertBefore方法
- insertBefore方法的功能和appendChild相似,都是将一个孩子节点连接到一个父亲节点,但insertBefore方法允许我们指定孩子节点的位置。insertBefore的语法是fatherObj.insertBefore(childObj,
- brotherObj),返回值是被连接的孩子节点。执行后,childObj的位置在brotherObj之前。来看看下面的例子:
- <HTML><HEAD><TITLE> DOM Demo </title></HEAD>
- <BODY ID="bodyNode">
- <SCRIPT LANGUAGE="JavaScript">
- <!--
- alert("页面初始状态");
- tableObj = document.createElement("TABLE");
- tbodyObj = document.createElement("TBODY");
- tr1Obj = document.createElement("TR");
- tr2Obj = tr1Obj.cloneNode();
- tr3Obj = tr1Obj.cloneNode();
- tr1td1Obj = document.createElement("TD");
- tr1td2Obj = tr1td1Obj.cloneNode();
- tr2td1Obj = tr1td1Obj.cloneNode();
- tr2td2Obj = tr1td1Obj.cloneNode();
- tr3td1Obj = tr1td1Obj.cloneNode();
- tr3td2Obj = tr1td1Obj.cloneNode();
- row1cell1Obj = document.createTextNode("This is row 1, cell 1");
- row1cell2Obj = row1cell1Obj.cloneNode();
- row2cell1Obj = row1cell1Obj.cloneNode();
- row2cell2Obj = row1cell1Obj.cloneNode();
- row3cell1Obj = row1cell1Obj.cloneNode();
- row3cell2Obj = row1cell1Obj.cloneNode();
- row1cell2Obj.nodeValue = "This is row 1, cell 2";
- row2cell1Obj.nodeValue = "This is row 2, cell 1";
- row2cell2Obj.nodeValue = "This is row 2, cell 2";
- row3cell1Obj.nodeValue = "This is row 3, cell 1";
- row3cell2Obj.nodeValue = "This is row 3, cell 2";
- returnValue = tableObj.insertBefore(tbodyObj);
- tbodyObj.insertBefore(tr3Obj);
- tbodyObj.insertBefore(tr2Obj, tr3Obj);
- tbodyObj.insertBefore(tr1Obj, tr2Obj);
- tr1Obj.insertBefore(tr1td2Obj);
- tr1Obj.insertBefore(tr1td1Obj, tr1td2Obj);
- tr2Obj.insertBefore(tr2td2Obj);
- tr2Obj.insertBefore(tr2td1Obj, tr2td2Obj);
- tr3Obj.insertBefore(tr3td2Obj);
- tr3Obj.insertBefore(tr3td1Obj, tr3td2Obj);
- tr1td2Obj.insertBefore(row1cell2Obj);
- tr1td1Obj.insertBefore(row1cell1Obj);
- tr2td2Obj.insertBefore(row2cell2Obj);
- tr2td1Obj.insertBefore(row2cell1Obj);
- tr3td2Obj.insertBefore(row3cell2Obj);
- tr3td1Obj.insertBefore(row3cell1Obj);
- bodyNode.insertBefore(tableObj);
- // -->
- </SCRIPT></BODY></HTML>
- 上面的代码又一次演示了一个Table的动态创建过程。首先创建Table的各个行、列以及单元格的节点,然后混合使用insertBefore将这些节点进行连接形成一个Table,最后通过语句bodyNode.
- insertBefore (tableObj)将Table装载进文档中。可以看到,如果省略掉第2个参数brotherObj采用fatherObj.insertBefore(childObj)方式,那么一定是在要连入的父亲节点还没有孩子节点的情况下,这时,就和appendNode方法的功能完全一样了。点击这里运行这个实例。
- 使用removeNode方法
- removeNode方法的功能是删除一个节点,语法为node.removeNode(false)或者node.removeNode(true),返回值是被删除的节点。removeNode(false)表示仅仅删除指定节点,然后这个节点的原孩子节点提升为原双亲节点的孩子节点。removeNode(true)表示删除指定节点及其所有下属节点。被删除的节点成为了孤立节点,不再具有有孩子节点和双亲节点。来看看下面的例子:
- <HTML><HEAD><TITLE>DOM Demo</TITLE></HEAD>
- <BODY id=bodyNode>This is the document body
- <P id=p1Node>This is paragraph 1.</P>
- <P id=p2Node>This is paragraph 2.</P>
- <P id=p3Node>This is paragraph 3. <IMG id=imgNode
- src="myexam.gif">This text follows the image
- <TABLE id=tableNode>
- <TBODY>
- <TR>
- <TD bgColor=yellow>This is row 1, cell 1</TD>
- <TD bgColor=orange>This is row 1, cell 2</TD></TR>
- <TR>
- <TD bgColor=red>This is row 2, cell 1</TD>
- <TD bgColor=magenta>This is row 2, cell 2</TD></TR>
- <TR>
- <TD bgColor=lightgreen>This is row 3, cell 1</TD>
- <TD bgColor=beige>This is row 3, cell 2</TD></TR></TBODY></TABLE></P>
- <P id=p4Node>This is paragraph 4.</P>
- <SCRIPT language=JavaScript>
- <!--
- var msg = "";
- function printChildren() {
- childCount = bodyNode.childNodes.length;
- msg += "bodyNode.childNodes.length = " + bodyNode.childNodes.length + "\n" ;
- for(var i = 0; i < childCount; i++) {
- msg += "bodyNode.childNodes[i].nodeName = " +
- bodyNode.childNodes[i].nodeName + "\n";
- }
- alert(msg);
评论
5 楼
xms_999
2007-07-18
谢谢分享
4 楼
hexiaodong
2007-04-10
qmei 写道
利用XMLDOM技术做的开源的web打印控件 www.fcsoft.com.cn/ePrint.htm
你们的打印控件(ocx)还可以,不过不喜欢其中的htc控件。所以单据打印和报表打印,我是自己写的js控件
3 楼
knight
2007-04-10
http://knight.iteye.com/admin/show/44413
这里就有手册可以下载了。。。
这里就有手册可以下载了。。。
2 楼
cozone_柯中
2007-04-10
麻烦你给个demo吧,这么大篇幅的代码,看的头疼
1 楼
AvengerBevis
2007-04-10
<div class='code_title'>java 代码</div>
<div class='dp-highlighter'>
<div class='bar'> </div>
<ol class='dp-j' start='1'>
<li class='alt'><span><span>} </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg=<span class='string'>"页面初始状态"</span><span> + </span><span class='string'>"\n\n"</span><span> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>printChildren(); </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg=<span class='string'>"对页面内容进行修改后"</span><span> + </span><span class='string'>"\n\n"</span><span> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg += <span class='string'>"Deleting Paragraph 3\n"</span><span>; </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>var deletedNode = p3Node.removeNode(<span class='keyword'>false</span><span>); </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg += <span class='string'>"deletedNode.nodeName = "</span><span> + deletedNode.nodeName + </span><span class='string'>"\n"</span><span>; </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg += <span class='string'>"deletedNode.childNodes.length = "</span><span> + </span></span></li>
<li class=''><span>deletedNode.childNodes.length + <span class='string'>"\n"</span><span>; </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span>printChildren(); </span></li>
<li class='alt'><span> </span></li>
<li class=''><span><span class='comment'>// --></span><span> </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span></SCRIPT></BODY></HTML> </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>上述代码首先显示页面的初始内容以及bodyNode的每个孩子节点的nodeName属性值,然后仅仅删除p3Node。删除p3Node前,bodyNode有<span class='number'>6</span><span>个孩子节点,p3Node有</span><span class='number'>4</span><span>个孩子节点。删除p3Node后,节点p3Node的</span><span class='number'>4</span><span>个孩子节点提升为bodyNode的孩子节点,这样bodyNode就有了</span><span class='number'>9</span><span>个节点。p3Node被删除后返回值为deletedNode,它的nodeName属性值为P,孩子节点数为</span><span class='number'>0</span><span>,也就是说,它成为一个孤立节点。点击这里运行这个实例。 </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>现在我们将上述代码中的deletedNode = p3Node.removeNode(<span class='keyword'>false</span><span>)修改为deletedNode = </span></span></li>
<li class=''><span>p3Node.removeNode(<span class='keyword'>true</span><span>),你会看到,删除p3Node后,bodyNode的孩子节点数变为</span><span class='number'>5</span><span>。点击这里看看实际效果。 </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span>JavaScript高级应用:使用DOM技术操纵文档 </span></li>
<li class='alt'><span>使用replaceNode方法 </span></li>
<li class=''><span> </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>replaceNode方法的功能是用新创建的节点替换一个节点及其下属节点,语法为oldNode.replaceNode(newNode),返回值是被替换的节点。来看看下面的例子: </span></li>
<li class='alt'><span> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><HTML><HEAD><TITLE> DOM Demo </title></HEAD> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><BODY ID=<span class='string'>"bodyNode"</span><span>> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>This is the document body </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><P ID = <span class='string'>"p1Node"</span><span>>This is paragraph </span><span class='number'>1</span><span>.</P> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><P ID = <span class='string'>"p2Node"</span><span>>This is paragraph </span><span class='number'>2</span><span>.</P> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><P ID = <span class='string'>"p3Node"</span><span>>This is paragraph </span><span class='number'>3</span><span>. </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><IMG ID = <span class='string'>"imgNode"</span><span> SRC=</span><span class='string'>"myexam.gif"</span><span>>This text follows the image </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TABLE ID=<span class='string'>"tableNode"</span><span>> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TR><TD BGCOLOR=yellow>This is row <span class='number'>1</span><span>, cell </span><span class='number'>1</span><span></TD><TD </span></span></li>
<li class=''><span>BGCOLOR=orange>This is row <span class='number'>1</span><span>, cell </span><span class='number'>2</span><span></TD></TR> </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span><TR><TD BGCOLOR=red>This is row <span class='number'>2</span><span>, cell </span><span class='number'>1</span><span></TD><TD BGCOLOR=magenta>This </span></span></li>
<li class='alt'><span>is row <span class='number'>2</span><span>, cell </span><span class='number'>2</span><span></TD></TR> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TR><TD BGCOLOR=lightgreen>This is row <span class='number'>3</span><span>, cell </span><span class='number'>1</span><span></TD><TD </span></span></li>
<li class=''><span>BGCOLOR=beige>This is row <span class='number'>3</span><span>, cell </span><span class='number'>2</span><span></TD></TR> </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span></TABLE> </span></li>
<li class='alt'><span> </span></li>
<li class=''><span></P> </span></li>
<li class='alt'><span> </span></li>
<li class=''><span><P ID = <span class='string'>"p4Node"</span><span>>This is paragraph </span><span class='number'>4</span><span>.</P> </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span><SCRIPT LANGUAGE=<span class='string'>"JavaScript"</span><span>> </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span><!-- </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>var msg = <span class='string'>""</span><span>; </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span>function printChildren() { </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>childCount = bodyNode.childNodes.length; </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>msg += <span class='string'>"bodyNode.childNodes.length = "</span><span> + bodyNode.childNodes.length + </span><span class='string'>"\n"</span><span> ; </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span><span class='keyword'>for</span><span>(var i = </span><span class='number'>0</span><span>; i < childCount; i++) { </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span>msg += <span class='string'>"bodyNode.childNodes[i].nodeName = "</span><span> + </span></span></li>
<li class='alt'><span>bodyNode.childNodes[i].nodeName + <span class='string'>"\n"</span><span>; </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>} </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>alert(msg); </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>} </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg=<span class='string'>"页面初始状态"</span><span> + </span><span class='string'>"\n\n"</span><span> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>printChildren(); </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg=<span class='string'>"对页面内容进行修改后"</span><span> + </span><span class='string'>"\n\n"</span><span> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg += <span class='string'>"Replacing Paragraph 3\n"</span><span>; </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>var b = document.createTextNode(<span class='string'>"New Body Page"</span><span>); </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>var replacedNode = p3Node.replaceNode(b); </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg += <span class='string'>"replacedNode.nodeName = "</span><span> + replacedNode.nodeName + </span><span class='string'>"\n"</span><span>; </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg += <span class='string'>"replacedNode.childNodes.length = "</span><span> + </span></span></li>
<li class=''><span>replacedNode.childNodes.length + <span class='string'>"\n"</span><span>; </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span>msg += <span class='string'>"p2Node.nodeName = "</span><span> + p2Node.nodeName + </span><span class='string'>"\n"</span><span>; </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span>printChildren(); </span></li>
<li class='alt'><span> </span></li>
<li class=''><span><span class='comment'>// --></span><span> </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span></SCRIPT></BODY></HTML> </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>上述代码首先显示页面的初始内容以及bodyNode的每个孩子节点的nodeName属性值,然后用新创建的文本项节点替换p3Node。替换后,bodyNode的孩子节点数目没有变,原来的P节点替换为文本项节点。变量replaceNode表示被替换的节点p3Node,它仍包含原来的<span class='number'>4</span><span>个孩子节点。点击这里运行这个实例。 </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>replaceNode方法的另一个功能是删除节点及其下属节点,就象前面介绍的removeNode(<span class='keyword'>true</span><span>)一样。这时,我们只需将replaceNode方法的参数设置为一个现存的节点就可以。现在,我们将上述代码中的b </span></span></li>
<li class=''><span>= document.createTextNode(<span class='string'>"New Body Page"</span><span>)修改为b = </span></span></li>
<li class='alt'><span>p2Node,你会看到,p3Node及其下属节点被删除了,没有替换节点生成,bodyNode的孩子节点数变为<span class='number'>5</span><span>。点击这里看看实际效果。 </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>如果replaceNode方法的参数为空,也就是执行replaceNode(),那么将导致浏览器脚本运行错误。 </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>JavaScript高级应用:使用DOM技术操纵文档 </span></li>
<li class='alt'><span>使用swapNode方法 </span></li>
<li class=''><span> </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>swapNode方法的功能是交换<span class='number'>2</span><span>个节点(包括其下属节点)在文档Tree中的位置,语法为firstNode.swapNode(secondNode),返回值是节点firstNode。来看看下面的例子: </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><HTML><HEAD><TITLE>DOM Demo</TITLE></HEAD> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><BODY id=bodyNode>This is the document body </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><P id=p1Node>This is paragraph <span class='number'>1</span><span>.</P> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><P id=p2Node>This is paragraph <span class='number'>2</span><span>.</P> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><P id=p3Node>This is paragraph <span class='number'>3</span><span>. <IMG id=imgNode </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>src=<span class='string'>"myexam.gif"</span><span>>This text follows the image </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TABLE id=tableNode> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TBODY> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TR> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TD bgColor=yellow>This is row <span class='number'>1</span><span>, cell </span><span class='number'>1</span><span></TD> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TD bgColor=orange>This is row <span class='number'>1</span><span>, cell </span><span class='number'>2</span><span></TD></TR> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TR> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TD bgColor=red>This is row <span class='number'>2</span><span>, cell </span><span class='number'>1</span><span></TD> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TD bgColor=magenta>This is row <span class='number'>2</span><span>, cell </span><span class='number'>2</span><span></TD></TR> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TR> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TD bgColor=lightgreen>This is row <span class='number'>3</span><span>, cell </span><span class='number'>1</span><span></TD> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TD bgColor=beige>This is row <span class='number'>3</span><span>, cell </span><span class='number'>2</span><span></TD></TR></TBODY></TABLE></P> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><P id=p4Node>This is paragraph <span class='number'>4</span><span>.</P> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><SCRIPT language=JavaScript> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><!-- </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>var msg = <span class='string'>""</span><span>; </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>function printChildren() { </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>childCount = bodyNode.childNodes.length; </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg += <span class='string'>"bodyNode.childNodes.length = "</span><span> + bodyNode.childNodes.length + </span><span class='string'>"\n"</span><span> ; </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><span class='keyword'>for</span><span>(var i = </span><span class='number'>0</span><span>; i < childCount; i++) { </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg += <span class='string'>"bodyNode.childNodes[i].nodeName = "</span><span> + </span></span></li>
<li class=''><span>bodyNode.childNodes[i].nodeName + <span class='string'>"\n"</span><span>; </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span>} </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>alert(msg); </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>} </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>msg=<span class='string'>"页面初始状态"</span><span> + </span><span class='string'>"\n\n"</span><span> </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span>printChildren(); </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>msg=<span class='string'>"对页面内容进行修改后"</span><span> + </span><span class='string'>"\n\n"</span><span> </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span>msg += <span class='string'>"Swapping Paragraph 3 with Paragraph 2\n"</span><span>; </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span>var b = p2Node; </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>var swappedNode = p3Node.swapNode(b); </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>msg += <span class='string'>"swappedNode.nodeName = "</span><span> + swappedNode.nodeName + </span><span class='string'>"\n"</span><span>; </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span>msg += <span class='string'>"swappedNode.childNodes.length = "</span><span> + </span></span></li>
<li class='alt'><span>swappedNode.childNodes.length + <span class='string'>"\n"</span><span>; </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg += <span class='string'>"p2Node.nodeName = "</span><span> + p2Node.nodeName + </span><span class='string'>"\n"</span><span>; </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>printChildren(); </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><span class='comment'>// --></span><span> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span></SCRIPT></BODY></HTML> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>上述代码首先显示页面的初始内容以及bodyNode的每个孩子节点的nodeName属性值,然后交换p3Node和p2Node的位置。交换后,bodyNode的孩子节点数目没有变,变量swappedNode表示p3Node,它仍包含原来的<span class='number'>4</span><span>个孩子节点。点击这里运行这个实例。 </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>swapNode方法的另一个功能是替换节点及其下属节点,就象前面介绍的replaceNode方法一样。这时,我们只需将replaceNode方法的参数设置为一个新创建的节点就可以。现在,我们将上述代码中的b </span></li>
<li class='alt'><span>= p2Node修改为b = document.createTextNode(<span class='string'>"This is a swapped in</span> </span></li>
<li class=''><span><span class='string'>text"</span><span>),你会看到,p3Node及其下属节点被新创建的文本项节点所替换,原来的节点类型P变为了文本项节点类型#text。点击这里看看实际效果。 </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>结 语 </span></li>
<li class=''><span> </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>以上我们详细讲解了JavaScript语言中在层次关系上操纵对象的各种DOM属性和方法原理,并且配以例程演示了实际的运行效果。我相信你已经完全掌握了如何使用这些方法,并会逐渐加深对DOM技术的喜爱。因为,DOM技术确实简化了文档对象的访问方式,为开发人员在对象层次上操纵文档提供了最便捷的手段。 </span></li>
</ol>
</div>
<div class='dp-highlighter'>
<div class='bar'> </div>
<ol class='dp-j' start='1'>
<li class='alt'><span><span>} </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg=<span class='string'>"页面初始状态"</span><span> + </span><span class='string'>"\n\n"</span><span> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>printChildren(); </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg=<span class='string'>"对页面内容进行修改后"</span><span> + </span><span class='string'>"\n\n"</span><span> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg += <span class='string'>"Deleting Paragraph 3\n"</span><span>; </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>var deletedNode = p3Node.removeNode(<span class='keyword'>false</span><span>); </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg += <span class='string'>"deletedNode.nodeName = "</span><span> + deletedNode.nodeName + </span><span class='string'>"\n"</span><span>; </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg += <span class='string'>"deletedNode.childNodes.length = "</span><span> + </span></span></li>
<li class=''><span>deletedNode.childNodes.length + <span class='string'>"\n"</span><span>; </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span>printChildren(); </span></li>
<li class='alt'><span> </span></li>
<li class=''><span><span class='comment'>// --></span><span> </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span></SCRIPT></BODY></HTML> </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>上述代码首先显示页面的初始内容以及bodyNode的每个孩子节点的nodeName属性值,然后仅仅删除p3Node。删除p3Node前,bodyNode有<span class='number'>6</span><span>个孩子节点,p3Node有</span><span class='number'>4</span><span>个孩子节点。删除p3Node后,节点p3Node的</span><span class='number'>4</span><span>个孩子节点提升为bodyNode的孩子节点,这样bodyNode就有了</span><span class='number'>9</span><span>个节点。p3Node被删除后返回值为deletedNode,它的nodeName属性值为P,孩子节点数为</span><span class='number'>0</span><span>,也就是说,它成为一个孤立节点。点击这里运行这个实例。 </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>现在我们将上述代码中的deletedNode = p3Node.removeNode(<span class='keyword'>false</span><span>)修改为deletedNode = </span></span></li>
<li class=''><span>p3Node.removeNode(<span class='keyword'>true</span><span>),你会看到,删除p3Node后,bodyNode的孩子节点数变为</span><span class='number'>5</span><span>。点击这里看看实际效果。 </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span>JavaScript高级应用:使用DOM技术操纵文档 </span></li>
<li class='alt'><span>使用replaceNode方法 </span></li>
<li class=''><span> </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>replaceNode方法的功能是用新创建的节点替换一个节点及其下属节点,语法为oldNode.replaceNode(newNode),返回值是被替换的节点。来看看下面的例子: </span></li>
<li class='alt'><span> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><HTML><HEAD><TITLE> DOM Demo </title></HEAD> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><BODY ID=<span class='string'>"bodyNode"</span><span>> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>This is the document body </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><P ID = <span class='string'>"p1Node"</span><span>>This is paragraph </span><span class='number'>1</span><span>.</P> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><P ID = <span class='string'>"p2Node"</span><span>>This is paragraph </span><span class='number'>2</span><span>.</P> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><P ID = <span class='string'>"p3Node"</span><span>>This is paragraph </span><span class='number'>3</span><span>. </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><IMG ID = <span class='string'>"imgNode"</span><span> SRC=</span><span class='string'>"myexam.gif"</span><span>>This text follows the image </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TABLE ID=<span class='string'>"tableNode"</span><span>> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TR><TD BGCOLOR=yellow>This is row <span class='number'>1</span><span>, cell </span><span class='number'>1</span><span></TD><TD </span></span></li>
<li class=''><span>BGCOLOR=orange>This is row <span class='number'>1</span><span>, cell </span><span class='number'>2</span><span></TD></TR> </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span><TR><TD BGCOLOR=red>This is row <span class='number'>2</span><span>, cell </span><span class='number'>1</span><span></TD><TD BGCOLOR=magenta>This </span></span></li>
<li class='alt'><span>is row <span class='number'>2</span><span>, cell </span><span class='number'>2</span><span></TD></TR> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TR><TD BGCOLOR=lightgreen>This is row <span class='number'>3</span><span>, cell </span><span class='number'>1</span><span></TD><TD </span></span></li>
<li class=''><span>BGCOLOR=beige>This is row <span class='number'>3</span><span>, cell </span><span class='number'>2</span><span></TD></TR> </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span></TABLE> </span></li>
<li class='alt'><span> </span></li>
<li class=''><span></P> </span></li>
<li class='alt'><span> </span></li>
<li class=''><span><P ID = <span class='string'>"p4Node"</span><span>>This is paragraph </span><span class='number'>4</span><span>.</P> </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span><SCRIPT LANGUAGE=<span class='string'>"JavaScript"</span><span>> </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span><!-- </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>var msg = <span class='string'>""</span><span>; </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span>function printChildren() { </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>childCount = bodyNode.childNodes.length; </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>msg += <span class='string'>"bodyNode.childNodes.length = "</span><span> + bodyNode.childNodes.length + </span><span class='string'>"\n"</span><span> ; </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span><span class='keyword'>for</span><span>(var i = </span><span class='number'>0</span><span>; i < childCount; i++) { </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span>msg += <span class='string'>"bodyNode.childNodes[i].nodeName = "</span><span> + </span></span></li>
<li class='alt'><span>bodyNode.childNodes[i].nodeName + <span class='string'>"\n"</span><span>; </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>} </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>alert(msg); </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>} </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg=<span class='string'>"页面初始状态"</span><span> + </span><span class='string'>"\n\n"</span><span> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>printChildren(); </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg=<span class='string'>"对页面内容进行修改后"</span><span> + </span><span class='string'>"\n\n"</span><span> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg += <span class='string'>"Replacing Paragraph 3\n"</span><span>; </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>var b = document.createTextNode(<span class='string'>"New Body Page"</span><span>); </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>var replacedNode = p3Node.replaceNode(b); </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg += <span class='string'>"replacedNode.nodeName = "</span><span> + replacedNode.nodeName + </span><span class='string'>"\n"</span><span>; </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg += <span class='string'>"replacedNode.childNodes.length = "</span><span> + </span></span></li>
<li class=''><span>replacedNode.childNodes.length + <span class='string'>"\n"</span><span>; </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span>msg += <span class='string'>"p2Node.nodeName = "</span><span> + p2Node.nodeName + </span><span class='string'>"\n"</span><span>; </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span>printChildren(); </span></li>
<li class='alt'><span> </span></li>
<li class=''><span><span class='comment'>// --></span><span> </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span></SCRIPT></BODY></HTML> </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>上述代码首先显示页面的初始内容以及bodyNode的每个孩子节点的nodeName属性值,然后用新创建的文本项节点替换p3Node。替换后,bodyNode的孩子节点数目没有变,原来的P节点替换为文本项节点。变量replaceNode表示被替换的节点p3Node,它仍包含原来的<span class='number'>4</span><span>个孩子节点。点击这里运行这个实例。 </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>replaceNode方法的另一个功能是删除节点及其下属节点,就象前面介绍的removeNode(<span class='keyword'>true</span><span>)一样。这时,我们只需将replaceNode方法的参数设置为一个现存的节点就可以。现在,我们将上述代码中的b </span></span></li>
<li class=''><span>= document.createTextNode(<span class='string'>"New Body Page"</span><span>)修改为b = </span></span></li>
<li class='alt'><span>p2Node,你会看到,p3Node及其下属节点被删除了,没有替换节点生成,bodyNode的孩子节点数变为<span class='number'>5</span><span>。点击这里看看实际效果。 </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>如果replaceNode方法的参数为空,也就是执行replaceNode(),那么将导致浏览器脚本运行错误。 </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>JavaScript高级应用:使用DOM技术操纵文档 </span></li>
<li class='alt'><span>使用swapNode方法 </span></li>
<li class=''><span> </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>swapNode方法的功能是交换<span class='number'>2</span><span>个节点(包括其下属节点)在文档Tree中的位置,语法为firstNode.swapNode(secondNode),返回值是节点firstNode。来看看下面的例子: </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><HTML><HEAD><TITLE>DOM Demo</TITLE></HEAD> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><BODY id=bodyNode>This is the document body </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><P id=p1Node>This is paragraph <span class='number'>1</span><span>.</P> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><P id=p2Node>This is paragraph <span class='number'>2</span><span>.</P> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><P id=p3Node>This is paragraph <span class='number'>3</span><span>. <IMG id=imgNode </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>src=<span class='string'>"myexam.gif"</span><span>>This text follows the image </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TABLE id=tableNode> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TBODY> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TR> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TD bgColor=yellow>This is row <span class='number'>1</span><span>, cell </span><span class='number'>1</span><span></TD> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TD bgColor=orange>This is row <span class='number'>1</span><span>, cell </span><span class='number'>2</span><span></TD></TR> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TR> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TD bgColor=red>This is row <span class='number'>2</span><span>, cell </span><span class='number'>1</span><span></TD> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TD bgColor=magenta>This is row <span class='number'>2</span><span>, cell </span><span class='number'>2</span><span></TD></TR> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TR> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TD bgColor=lightgreen>This is row <span class='number'>3</span><span>, cell </span><span class='number'>1</span><span></TD> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><TD bgColor=beige>This is row <span class='number'>3</span><span>, cell </span><span class='number'>2</span><span></TD></TR></TBODY></TABLE></P> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><P id=p4Node>This is paragraph <span class='number'>4</span><span>.</P> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><SCRIPT language=JavaScript> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><!-- </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>var msg = <span class='string'>""</span><span>; </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>function printChildren() { </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>childCount = bodyNode.childNodes.length; </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg += <span class='string'>"bodyNode.childNodes.length = "</span><span> + bodyNode.childNodes.length + </span><span class='string'>"\n"</span><span> ; </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span><span class='keyword'>for</span><span>(var i = </span><span class='number'>0</span><span>; i < childCount; i++) { </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg += <span class='string'>"bodyNode.childNodes[i].nodeName = "</span><span> + </span></span></li>
<li class=''><span>bodyNode.childNodes[i].nodeName + <span class='string'>"\n"</span><span>; </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span>} </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>alert(msg); </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>} </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>msg=<span class='string'>"页面初始状态"</span><span> + </span><span class='string'>"\n\n"</span><span> </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span>printChildren(); </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>msg=<span class='string'>"对页面内容进行修改后"</span><span> + </span><span class='string'>"\n\n"</span><span> </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span>msg += <span class='string'>"Swapping Paragraph 3 with Paragraph 2\n"</span><span>; </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span>var b = p2Node; </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>var swappedNode = p3Node.swapNode(b); </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>msg += <span class='string'>"swappedNode.nodeName = "</span><span> + swappedNode.nodeName + </span><span class='string'>"\n"</span><span>; </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span>msg += <span class='string'>"swappedNode.childNodes.length = "</span><span> + </span></span></li>
<li class='alt'><span>swappedNode.childNodes.length + <span class='string'>"\n"</span><span>; </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>msg += <span class='string'>"p2Node.nodeName = "</span><span> + p2Node.nodeName + </span><span class='string'>"\n"</span><span>; </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span>printChildren(); </span></li>
<li class=''><span> </span></li>
<li class='alt'><span><span class='comment'>// --></span><span> </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span></SCRIPT></BODY></HTML> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>上述代码首先显示页面的初始内容以及bodyNode的每个孩子节点的nodeName属性值,然后交换p3Node和p2Node的位置。交换后,bodyNode的孩子节点数目没有变,变量swappedNode表示p3Node,它仍包含原来的<span class='number'>4</span><span>个孩子节点。点击这里运行这个实例。 </span></span></li>
<li class=''><span> </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>swapNode方法的另一个功能是替换节点及其下属节点,就象前面介绍的replaceNode方法一样。这时,我们只需将replaceNode方法的参数设置为一个新创建的节点就可以。现在,我们将上述代码中的b </span></li>
<li class='alt'><span>= p2Node修改为b = document.createTextNode(<span class='string'>"This is a swapped in</span> </span></li>
<li class=''><span><span class='string'>text"</span><span>),你会看到,p3Node及其下属节点被新创建的文本项节点所替换,原来的节点类型P变为了文本项节点类型#text。点击这里看看实际效果。 </span></span></li>
<li class='alt'><span> </span></li>
<li class=''><span> </span></li>
<li class='alt'><span>结 语 </span></li>
<li class=''><span> </span></li>
<li class='alt'><span> </span></li>
<li class=''><span>以上我们详细讲解了JavaScript语言中在层次关系上操纵对象的各种DOM属性和方法原理,并且配以例程演示了实际的运行效果。我相信你已经完全掌握了如何使用这些方法,并会逐渐加深对DOM技术的喜爱。因为,DOM技术确实简化了文档对象的访问方式,为开发人员在对象层次上操纵文档提供了最便捷的手段。 </span></li>
</ol>
</div>
相关推荐
总之,DOM解析XML文档是编程中常用的技术,它提供了一种强大且灵活的方式来处理XML数据。理解和掌握DOM解析,对于任何涉及XML操作的开发工作都至关重要。通过熟练运用DOM,我们可以高效地构建和操纵XML文档,实现...
通过结合DOM接口和Jscript脚本,可以高效地操纵SVG文档,实现实时更新和动态展示的功能。这种方法不仅适用于网络化测试系统的测试结果展示,还可以广泛应用于各种需要动态图形的场景,如在线地图服务、数据可视化等...
总的来说,这三份文档构成了一套完整的前端开发参考工具,涵盖了从编写JavaScript代码到应用CSS样式,再到通过DOM操纵页面元素的全过程。对于想要深入学习Web前端技术或者提升现有技能的开发者来说,这些文档无疑是...
这篇教程——"JavaScript学习手册十四:HTML DOM-文档元素的操作(二)"深入探讨了如何使用JavaScript来操纵HTML文档中的元素,进一步提升网页动态性和交互性。 DOM是HTML和XML文档的一种结构化表示,它将网页内容...
### 使用VB操纵XML文档 #### 一、XML概述 XML(Extensible Markup Language,可扩展标记语言)作为一种标准的信息存储格式,在IT领域中...随着XML在各个领域的广泛应用,掌握使用VB操纵XML文档的技术变得尤为重要。
利用 DOM 技术,操纵文档内容的属性和方法具备了与元素无关性特点,这样能够按相同的步骤操纵文档内容。DOM 树中的所有节点都是从 Node 对象继承而来的。Node 对象定义了一些最基本的属性和方法,利用这些方法可以...
【Ajax第5部分-操纵DOM】是关于使用JavaScript来实时更新Web页面的技术。DOM,即文档对象模型,是Web页面的结构化表示,它将HTML和CSS转化为一系列可操作的对象。DOM树是DOM的一种可视化表示,它包含了页面的所有...
在“掌握Ajax”系列的第五部分中,我们深入探讨了如何使用JavaScript操纵文档对象模型(DOM),以实现实时更新Web页面,而无需进行完整的页面重载。这一技术的核心在于理解和操作DOM树,即浏览器解析HTML和CSS后构建...
### 使用DOM解析XML #### 一、概述:API在XML应用程序中的作用 在开发XML应用程序时,解析XML文档是一项核心任务。这不仅涉及到识别元素标签、属性、注释和指令,还包括解析实体,以便能够访问文档的各个语法成分...
当需要在Firefox浏览器环境下处理XML文档时,开发者通常会使用XML DOM(Document Object Model)来解析和操作XML。本文将详细讲解如何在Firefox下利用XML DOM解析XML文档。 首先,理解XML DOM的概念是至关重要的。...
DOM将一个文档结构化为一个由节点组成的树形模型,允许开发者通过JavaScript或其他脚本语言来访问、操纵、更新以及呈现文档内容。在“fifdoc_html”这个压缩包中,我们可能找到了关于DOM开发的详细文档,特别是针对...
在IT领域,DOM(Document Object Model)表格操纵是前端开发中的一个重要主题,它涉及到如何使用JavaScript来操作HTML表格。DOM是一种标准,允许程序和脚本动态更新、添加、删除和改变HTML或XML文档的结构。在这个...
`Dom4j使用简介.pdf`提供了Dom4J的基本使用教程,涵盖了如何创建、解析、修改XML文档,以及如何使用XPath等内容。新手可以通过阅读这份指南快速上手。 总之,Dom4J 1.6.1是处理XML文档的得力工具,无论是在小型项目...
《JavaScript DOM编程艺术(第2版)》这本书详细介绍了如何使用JavaScript来操纵DOM,实现网页的动态交互效果。通过深入学习本书中的内容,开发者可以更好地理解和掌握JavaScript与DOM的结合方式,提高Web开发的能力...
在"Dom文档对象模型-2010版"这个资源中,包含了对DOM的详细解释和使用方法,尤其对于JavaScript开发者来说,这是一个非常实用的手册。它可能涵盖了以下主要知识点: 1. **DOM元素节点**:DOM中的每个HTML元素都是一...
**DOM文档对象中文手册** DOM(Document Object Model)是一种国际标准,它定义了一种结构化表示HTML或XML...DOM文档对象中文手册提供了详细的方法和示例,帮助开发者更有效地操纵网页内容,提升网页应用的用户体验。
3. **添加元素**:使用`createElement()`创建新的DOM节点,然后用`appendChild()`将其添加到文档中。 4. **删除元素**:使用`removeChild()`从文档中移除节点。 #### DOM 中的重要数据类型 在DOM中,有几个关键的...
DOM(文档对象模型)是处理XML文档的一种主要方法,它将XML文档视为一个可以被程序操纵的树形结构。在.NET环境中,我们可以使用System.Xml命名空间中的类来操作DOM。 标题"XmlDom.rar_DOM_dom xml"暗示我们将讨论...
JavaScript和DOM Enlightenment是关于Web开发中的核心技术的深入学习资源,主要涵盖了JavaScript编程语言以及文档对象模型(Document Object Model)的应用。这两个概念对于任何希望成为前端开发者的人来说都是至关...
HTML DOM(Document Object Model)是...总的来说,HTML DOM是前端开发的核心技术之一,它允许我们以编程方式操纵网页,实现丰富的交互性和动态更新。理解并熟练掌握DOM操作,是成为一名合格的Web开发者所必需的技能。