在指定的子节点之前添加一个节点

  1. <html>
  2. <head>
  3. <script type="text/javascript" src="/example/xdom/loadxmldoc.js">
  4. </script>
  5. </head>
  6. <body>
  7.  
  8. <script type="text/javascript">
  9. xmlDoc=loadXMLDoc("/example/xdom/books.xml");
  10.  
  11. newNode=xmlDoc.createElement("book");
  12.  
  13. x=xmlDoc.documentElement;
  14. y=xmlDoc.getElementsByTagName("book");
  15.  
  16. document.write("Book elements before: " + y.length);
  17. document.write("<br />");
  18. x.insertBefore(newNode,y[3]);
  19.  
  20. y=xmlDoc.getElementsByTagName("book");
  21. document.write("Book elements after: " + y.length);
  22. </script>
  23. </body>
  24. </html>