我的腳本正在從XML文件加載一些數據並用它打印一張表。'undefined'在javascript函數調用後打印
function draw_schedule() {
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","schedule.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.write("<table width='100%' border='1'>");
var x=xmlDoc.getElementsByTagName("day");
for (i=0;i<x.length;i++) {// number of days
document.write("<tr><th colspan='2'>");
document.write(x[i].getElementsByTagName("date")[0].childNodes[0].nodeValue);// the date for each day
document.write("</th></tr>");
var y=x[i].getElementsByTagName("session");// daily sessions
for (j=0;j<y.length;j++) {
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("title")[j].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("time")[j].childNodes[0].nodeValue);
document.write("</td></tr>");
}
}
document.write("</table>");
}
如果我從HTML文件中調用函數(單獨文件),它會打印表格,然後打印'undefined'。如果我將腳本嵌入到HTML中,它將打印表格而不打印「未定義」。我無法弄清楚爲什麼在一個單獨的文件中腳本會改變它的行爲。我會喜歡比我更聰明的人解釋。謝謝!
你完全確定使用`文件撰寫()`是你想要做什麼?由於很多原因,這是構建頁面內容的一個非常不受歡迎的方式。 – Pointy 2010-12-04 22:10:42