Javascript 用数组中的值填充表行

Javascript 用数组中的值填充表行,javascript,dom,html-table,Javascript,Dom,Html Table,我正在使用JavaScriptDOM创建一个表,我需要将数组中的所有值(thValues)插入到TR(表行)中包含的每个TH中。我怎么能做到 让thValues=[“文本1”、“文本2”、“文本3”、“文本4”、“文本5”、“文本6”]; 让pickTableZone=document.getElementById(“tableDetails”); 让table=document.createElement(“table”); pickTableZone.appendChild(表); set

我正在使用JavaScriptDOM创建一个表,我需要将数组中的所有值(thValues)插入到TR(表行)中包含的每个TH中。我怎么能做到

让thValues=[“文本1”、“文本2”、“文本3”、“文本4”、“文本5”、“文本6”];
让pickTableZone=document.getElementById(“tableDetails”);
让table=document.createElement(“table”);
pickTableZone.appendChild(表);
setAttribute(“类”,“表-表-表-条带化”);
让tableBody=document.createElement(“tbody”);
table.appendChild(表体)//将tbody添加到表中
//对于thValues的长度,请创建表行
for(设i=0;i
表,th,td{
边框:1px纯色灰色;
边界塌陷:塌陷;
填充:1rem;
}

您需要两个循环:一个是用
th
单元格填充首行;一个是使用
td
单元格创建正文行。目前,您正在为每个标题值创建一个新行,而不是将所有标题值放在一行中。你也把你的头细胞放在
tbody
而不是
thead

下面是创建标题行的方法

const thValues=[“文本1”、“文本2”、“文本3”、“文本4”、“文本5”、“文本6”];
const pickTableZone=document.getElementById(“tableDetails”);
const table=document.createElement(“表”);
pickTableZone.appendChild(表);
setAttribute(“类”,“表-表-表-条带化”);
const tableHead=document.createElement(“thead”);
表1.儿童(表头)//将AD添加到表中
const headlow=document.createElement(“tr”);
床头。儿童(头枕);//第一行为头细胞。
//添加每个标题。
for(设i=0;i
表,th,td{
边框:1px纯色灰色;
边界塌陷:塌陷;
填充:1rem;
}

为了实现我的目标,我意识到我的代码中缺少了什么。我忘了在
let text=document.createTextNode(thValues[I])中添加一个I

让thValues=[“文本1”、“文本2”、“文本3”、“文本4”、“文本5”、“文本6”];
让pickTableZone=document.getElementById(“tableDetails”);
让table=document.createElement(“table”);
pickTableZone.appendChild(表);
setAttribute(“类”,“表-表-表-条带化”);
让tableBody=document.createElement(“tbody”);
table.appendChild(表体)//将tbody添加到表中
//对于thValues的长度,请创建表行
for(设i=0;i
表,th,td{
边框:1px纯色灰色;
边界塌陷:塌陷;
填充:1rem;
}

谢谢您的详细解释!我意识到我的代码中缺少了什么,我忘了在document.createTextNode(thValues[I])中将零替换为I