gpt4 book ai didi

javascript - 使用 Javascript 数组创建 HTML 表格?

转载 作者:行者123 更新时间:2023-11-30 21:16:03 26 4
gpt4 key购买 nike

我被分配了一项实验室作业。问题如下: Create Table Using JS Arrays

我想出了以下代码:

<!DOCTYPE html>
<html>
<head>
<title> Manan Tyagi 16BCE1240</title>

<p style="text-align:center;color:purple;font-size:30px" >Coordinates Of State Capitals</p>
<p id="demo"></p>

<script>
var capital=["Montgomery","Juneau","Phoenix","Little Rock","Sacramento","Denver","Hartford","Dover","Tallahassee","Atlanta"];
var state=["Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","Florida","Georgia"];
var code=["AL","AK","AZ","AR","CA","CO","CT","DE","FL","GA"];
var latitude=[32,58,33,35,38,40,42,39,31,34];
var longitude=[-86,-134,-113,-92,-121,-105,-73,-76,-84,-84];
var htmlstr="<tbody>";
for(var i=0;i<10;++i)
{
htmlstr+="<tr>"+"<td>"+capital[i]+"</td>"+"<td>"+state[i]+"</td>"+"<td>"+code[i]+"</td>"+"<td>"+latitude[i]+"</td>"+"<td>"+longitude[i]+"</td>"+"</tr>"+"<br>";
}
htmlstr+="</tbody>";
document.write(htmlstr);
</script>

</body>
</head>
</html>

我使用 document.write() 函数只是为了显示获得的输出。我想创建如图所示的表格吗?我应该在代码中更改什么地方以使其正常运行?

谢谢。

[编辑] 我担心排序部分。如何仅根据特定列对表格进行排序?

最佳答案

您错过了包括 <table>标签。引用http://html.com/tables/

<!DOCTYPE html>
<html>

<head>
<title> Manan Tyagi 16BCE1240</title>

<p style="text-align:center;color:purple;font-size:30px">Coordinates Of State Capitals</p>
<p id="demo"></p>

<script>
var capital = ["Montgomery", "Juneau", "Phoenix", "Little Rock", "Sacramento", "Denver", "Hartford", "Dover", "Tallahassee", "Atlanta"];
var state = ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia"];
var code = ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA"];
var latitude = [32, 58, 33, 35, 38, 40, 42, 39, 31, 34];
var longitude = [-86, -134, -113, -92, -121, -105, -73, -76, -84, -84];
var htmlstr = "<table><tbody>";
for (var i = 0; i < 10; ++i) {
htmlstr += "<tr>" + "<td>" + capital[i] + "</td>" + "<td>" + state[i] + "</td>" + "<td>" + code[i] + "</td>" + "<td>" + latitude[i] + "</td>" + "<td>" + longitude[i] + "</td></tr>";
}
htmlstr += "</tbody></table>";
document.write(htmlstr);
</script>
</head>

<body>
</body>


</html>

关于javascript - 使用 Javascript 数组创建 HTML 表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45670949/

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