我对此非常陌生,我正在上课,因为现在变得非常头疼。我有这个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>
答案 0 :(得分: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);