gpt4 book ai didi

Javascript 数组创建表

转载 作者:行者123 更新时间:2023-12-02 13:48:10 25 4
gpt4 key购买 nike

我正在尝试使用数组创建一个表作为学校的计划大纲。如何使第二个数组(num)在类(class)标题旁边排列。现在它只是堆叠在同一列中的它们下面......

<script>

var titl = ["Oral Communication","Applications/Concepts","SQL Programming","HTML and Dreamweaver","Javascript","Flash","XML","Cascading Style Sheets","XSL","ASP.NET","PHP/MySQL","Windows Operating Systems","Digital Imaging with Photoshop","Web Development Internship"];
var num = ["ENG171","CIT110","CIT236","CMT111","CMT113","CMT115","CMT117","CMT125","CMT211-see note*","CMT215","CMT241","CIT268","VMA105","CMT299"];

document.write('<table>')
document.write('<tr><th>Web Development Concentration Courses</th></tr>');

for (var i = 0; i < titl.length; i++)
{
document.write('<tr><td>' + titl[i] + '</td></tr>');
}

for (var j = 0; j < num.length; j++)
{
document.write('<tr><td>' + num[j] + '</td></tr>');
}
document.write('</table>');

</script>

最佳答案

构建 HTML 时不能拆分内容。另外,请不要使用document.write ,因为它很危险。

此外,主要的解决方案是组合循环。我通过检查两个数组的长度是否相同来做到这一点:

var titl = ["Oral Communication", "Applications/Concepts", "SQL Programming", "HTML and Dreamweaver", "Javascript", "Flash", "XML", "Cascading Style Sheets", "XSL", "ASP.NET", "PHP/MySQL", "Windows Operating Systems", "Digital Imaging with Photoshop", "Web Development Internship"];

var num = ["ENG171", "CIT110", "CIT236", "CMT111", "CMT113", "CMT115", "CMT117", "CMT125", "CMT211-see note*", "CMT215", "CMT241", "CIT268", "VMA105", "CMT299"];

var finalHTML = '<table>';
finalHTML += '<tr><th>Web Development Concentration Courses</th></tr>';

if (titl.length == num.length)
for (var i = 0; i < titl.length; i++) {
finalHTML += '<tr><td>' + titl[i] + '</td><td>' + num[i] + '</td></tr>';
}

finalHTML += '</table>';

document.getElementById("container").innerHTML = finalHTML;
<div id="container"></div>

预览

preview

另外,给出 <th>一个colspan="2"会给你更好的结果:

finalHTML += '<tr><th colspan="2">Web Development Concentration Courses</th></tr>';

preview

关于Javascript 数组创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41193683/

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