gpt4 book ai didi

javascript - 想在html中的div中显示xml文件数据

转载 作者:行者123 更新时间:2023-11-28 12:09:17 26 4
gpt4 key购买 nike

我有 java 脚本代码,我只想使用 html 在 div 中显示 xml 数据,这样我就可以使用 div 在 html 中显示 xml 数据的内容

    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", "cd_catalog.xml", false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;

document.write("<table border='1'>");
var x = xmlDoc.getElementsByTagName("CD");
for (i = 0; i < x.length; i++) {
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
</script>




</head>
<body onload="displayCD(2)">
<div id='showCD'>
</div>

最佳答案

不要使用document.write

获取你想要的div,比如:

var the_div = document.getElementByI("showCD");

然后设置它的内容(使用 TextNode):

var text_node = document.createTextNode(xmlDoc);
the_div.appendChild(text_node);

或者 xmlhttp.responseText 而不是 .responseXML。我不知道 responseXML 是否标准。

关于javascript - 想在html中的div中显示xml文件数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19585549/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com