【发布时间】:2015-06-09 06:06:00
【问题描述】:
我有一个 html 文档,它创建了一个包含数据的表格,由 xml 存档提供。在adobe括号的实时预览中,我可以看到页面和数据。
但是,当我尝试在某些浏览器(如 IE、Firefox 或 Chrome)中打开时,页面显示为空白。
这里是html代码的一部分,下面是xml代码。
这是html代码
<!DOCTYPE html>
<html>
<head>
<style>
table { border-collapse: collapse; font-family: Futura, Arial, sans-serif; border: 1px solid #777; }
caption { font-size: larger; margin: 1em auto; }
th, td { padding: .65em; }
th, thead { background: #000; color: #fff; border: 1px solid #000; }
tr:nth-child(odd) { background: #ccc; }
tr:hover { background: #aaa; }
td { border-right: 1px solid #777; }
</style>
</head>
<body>
<script>
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","prueba_new.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.write("<table><tr><th>CardN</th><th>CardC</th><th>CardE</th><th>CardNe</th></tr>");
var x=xmlDoc.getElementsByTagName("CCard");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("CardN")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("CardC")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("CardE")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("CardNe")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
</script>
</body>
</html>
这里是xml代码,当然,实际的xml文件,包含几百个条目。
<?xml version="1.0"?>
<numbers>
<CCard>
<CardNe>Volkervun</CardNe>
<CardN>10000034999</CardN>
<CardC>Red Team</CardC>
<CardE>Volker.vcd</CardE>
</CCard>
<CCard>
<CardNe>Lady Mars</CardNe>
<CardN>10009899899</CardN>
<CardC>Blue Team</CardC>
<CardE>LadyM.vcd</CardE>
</CCard>
</numbers>
我尝试了几件事,但都没有奏效。 对那个空白页有什么想法?
【问题讨论】:
-
document.write,浏览器还支持吗?更好的方法是在内存中构建整个表(在字符串变量中),然后使用[the container element].innerHTML = [the string variable];将其放入容器元素中 -
虽然 document.write 是丑陋且过时的技术,但我不认为它是罪魁祸首。您是否在开发者控制台中看到任何脚本错误?您可以尝试在 之后和 之前立即放置一些静态文本以确认页面已正确加载吗?
-
听起来您正试图在不使用本地 Web 服务器的情况下打开文件。安全性会以这种方式限制 ajax
-
@charlietfl 即使它在同一服务器上的同一目录中?
-
一直很抱歉。好吧,xml文档与页面位于同一目录中。但仍然根本不给桌子充电。你能解释更多的 .innerHTML 吗?
标签: javascript jquery html css xml