0

我对此完全陌生,我正在上一堂有趣的课,现在已经变成了一个大难题。我有这个 html DOM 文件,它引用了我需要在浏览器屏幕上显示的 XML 文件。它应该只打印标题、第一、最后、位置、描述的元素。CSS 和 Schema 都可以正常工作。当我使用 html 文件时,是否还应该将它们都保留在 xml 文件中?我在浏览器屏幕上得到一个空白。谢谢,利亚姆

<html>

<head>

    <title>Catalog</title>

    <script type="text/javascript">

         var xmlDoc;

         function loadXMLDoc() {

              // XML loader for IE

              if (window.ActiveXObject) {

                  xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

                  xmlDoc.load("catalog.xml");

                 printCatalog();

            }

            // XML loader for other browsers

           else {

                xmlDoc = document.implementation.createDocument("", "", null);

                xmlDoc.load("catalog.xml");

                xmlDoc.onload = printCatalog;

          }
      }


      function printcatalog() {

          var titleNodes = xmlDoc.getElementsByTagName("title");

          var firstNodes = xmlDoc.getElementsByTagName("first");

          var lastNodes = xmlDoc.getElementsByTagName("last");

          var locationNodes = xmlDoc.getElementsByTagName("location");

         var descriptionNodes = xmlDoc.getElementsByTagName("description");

            for (var i = 0; i < titleNodes.length; i++) {

              document.write("<div style='font-family:arial; font-weight:bold;

                                      color:red'>" + 

                                    titleNodes[i].firstChild.nodeValue + "</div>");

              document.write("<div style='font-family:arial'>" +

                                     firstNodes[i].firstChild.nodeValue + "<br />");

              document.write(lastNodes[i].firstChild.nodeValue + "<br />");

             document.write(firstNodes[i].firstChild.nodeValue + "<br />");

             document.write(locationNodes[i].firstChild.nodeValue + "<br />");

             document.write(descriptionNodes[i].firstChild.nodeValue + "</div>");
           }


       }

 </script>

 </head>

<body onload="loadXMLDoc()">

</body>

</html>







 <?xml version="1.0"?>

 <?xml-stylesheet href="catalog.css" type="text/css"?>

 <catalog xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"

 xs:noNamespaceSchemaLocation="catalog.xsd"> 

 <course courseNum="CS205" area="ComputerScience">

 <title>Programming 1</title>

 <instructor>

 <first>Bill</first>

 <last>Gates</last>

 </instructor>

 <units>3</units>

<location>

<building>100</building>

<room>101</room>

</location>

 <description>Learn from the man himself, Mr. Microsoft. 

             Must love PCs and not Macs.</description>

</course>

<course courseNum="SS305" area="SocialStudies">

<title>Government 1945</title>

<instructor>

 <first>Karl</first>

 <last>Marx</last>

 </instructor>

<units>3</units>

<location>

<building>200</building>

<room>202</room>

</location>

<description>Mr. Marx will explain his political theories

          Lecture course. Bring lots of coffee.</description>
 </course>

<course courseNum="MA350" area="Math">

<title>Math 911</title>

 <instructor>

 <first>Albert</first>

 <last>Einstein</last>

 </instructor>

 <units>3</units>

<location>

<building>200</building>

 <room>302</room>

</location>

 <description>Find the answers to the universe in just one semester.

  </description>

</course>

  </catalog>
4

1 回答 1

0

页面加载后不能使用document.write 因为它会覆盖已经加载的 DOM。

您应该执行以下操作:

<body>
   <script> 
       loadXMLDoc(); // call it while the page is loading
   </script>
</body>

或者,如果您不想调用 loadXMLDoc(); 早期,您可以将document.write更改为其他内容。可能是一个丑陋的版本是:

document.body.innerHTML += 'somestuff'

或将文本节点附加到正文:

var doc = document.createTextNode('text here');
document.body.appendChild(doc);
于 2010-11-09T11:13:31.510 回答