gpt4 book ai didi

javascript表格格式输出

转载 作者:行者123 更新时间:2023-11-28 00:51:25 26 4
gpt4 key购买 nike

我有这个用于获取表格格式输出的 JavaScript html 代码。我将 XML 数据作为脚本的输入。

function myFunction() {
parser=new DOMParser();
xmlDoc=parser.parseFromString(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+"<book>"+
"<title>The Time Machine and Other Stories</title>"+
"<author_name>H. G. Wells</author_name>"+
"<publish_date>2016</publish_date>"+
"<cost currency=\"USD\">10</cost>"+
"<publisher_info>"+
"<publisher_name>Read Books Ltd</publisher_name>"+
"<publisher_address>"+
"<street_name>Evesham street</street_name>"+
"<city>Worcestershire</city>"+
"<zip_code>11WR</zip_code>"+
"<country>United Kingdom</country>"+
"</publisher_address>"+
"</publisher_info>"+
"</book>","text/xml");

var x=xmlDoc.getElementsByTagName("publisher_address");

for (i=0;i<x.length;i++) {
document.write("<tr>");
var y=x[i].childNodes;
for (j=0;j<y.length;j++) {
document.write("<td>"+ y[j].childNodes[0].nodeValue+ "</td>");
}
document.write("</tr>");
}
}
table, th, td {
border: 1px solid black;
}
<h1>My First XML Parsing JavaScript</h1>
<p>Click the button to display book info.</p>
<p id="demo"></p>
<button type="button" onclick="myFunction()">Try it</button>
<table border="0">
</table>

我得到的输出是:

Evesham streetWorcestershire11WRUnited Kingdom

谁能帮我解决为什么我无法获得表格格式的输出?

最佳答案

您正在使用 document.write(),它没有给您预期的输出。

使用 createElement 创建一个表,通过创建行填充数据,然后将 td 和附加到您的 dom 或您的段落。

<!DOCTYPE html>
<html>

<head>
<style>
table,
th,
td {
border: 1px solid black;
}
</style>
</head>

<body>

<h1>My First XML Parsing JavaScript</h1>
<p>Click the button to display book info.</p>
<p id="demo"></p>

<button type="button" onclick="myFunction()">Try it</button>
<table border="0">
<script>
function myFunction() {
parser = new DOMParser();
xmlDoc = parser.parseFromString(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<book>" +
"<title>The Time Machine and Other Stories</title>" +
"<author_name>H. G. Wells</author_name>" +
"<publish_date>2016</publish_date>" +
"<cost currency=\"USD\">10</cost>" +
"<publisher_info>" +
"<publisher_name>Read Books Ltd</publisher_name>" +
"<publisher_address>" +
"<street_name>Evesham street</street_name>" +
"<city>Worcestershire</city>" +
"<zip_code>11WR</zip_code>" +
"<country>United Kingdom</country>" +
"</publisher_address>" +
"</publisher_info>" +
"</book>", "text/xml");

// creating a table
table = document.createElement('table');

var x = xmlDoc.getElementsByTagName("publisher_address");
for (i = 0; i < x.length; i++) {

// create a row
var tr = table.insertRow();

var y = x[i].childNodes;
for (j = 0; j < y.length; j++) {

// crate a cell for your data
var td = tr.insertCell();

// put the data into your td
td.innerHTML = y[j].childNodes[0].nodeValue;

}
}
// append it to body, or to your p#demo
document.body.appendChild(table);

}
</script>
</table>
</body>

</html>

关于javascript表格格式输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47120333/

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