删除元素节点
- <html>
-
- <head>
- <script type="text/javascript" src="/example/xdom/loadxmldoc.js">
- </script>
- </head>
-
- <body>
- <script type="text/javascript">
- //检查最后一个节点是否是元素节点
- function get_lastchild(n)
- {
- var x=n.lastChild;
- while (x.nodeType!=1)
- {
- x=x.previousSibling;
- }
- return x;
- }
-
- xmlDoc=loadXMLDoc("/example/xdom/books.xml");
-
- document.write("book 节点的数目:");
- document.write(xmlDoc.getElementsByTagName('book').length);
- document.write("<br />");
-
- var lastNode=get_lastchild(xmlDoc.documentElement);
- var delNode=xmlDoc.documentElement.removeChild(lastNode);
-
- document.write("removeChild() 方法执行后 book 节点的数目:");
- document.write(xmlDoc.getElementsByTagName('book').length);
-
- </script>
- </body>
- </html>