let text1 = "";
let text2 = "";
const tbl = document.createElement("table");
const tblBody = document.createElement("tbody");
for (let i = 0; i < 4; i++) {
text1 = shoppingList[2*i];
text2 = shoppingList[2*i+1];
const row = document.createElement("tr");
for (let j = 0; j < 2; j++) {
const cell = document.createElement("td");
const cellText = document.createTextNode(text1);
const cellText2 = document.createTextNode(text2);
cell.appendChild(cellText);
cell.appendChild(cellText2);
row.appendChild(cell);
}
tblBody.appendChild(row);
}
tbl.appendChild(tblBody);
document.body.appendChild(tbl);
tbl.setAttribute("border", "2");
是这样的,我有一个微整里的列表,奇数行是物品名称,偶数行是物品数量。所以
text1 = shoppingList[2i];
text2 = shoppingList[2i+1];
但是创建列表的时候两列,我希望一列是名称一列是数量,可是现在两列东西是重复的,第一列是物品名称和数量在一起,第二列也是这样的,要怎么做才能分开两列一列物品一列数量呀。谢谢大家了!!
创建两个 td,每个td 塞对应的数据,而不是一个td 塞两份数据。
如下:
let text1 = "";
let text2 = "";
const tbl = document.createElement("table");
const tblBody = document.createElement("tbody");
for (let i = 0; i < 4; i++) {
text1 = shoppingList[2*i];
text2 = shoppingList[2*i+1];
const row = document.createElement("tr");
for (let j = 0; j < 2; j++) {
const cell = document.createElement("td");
const cell1 = document.createElement("td");
const cellText = document.createTextNode(text1);
const cellText2 = document.createTextNode(text2);
cell.appendChild(cellText);
cell1.appendChild(cellText2);
row.appendChild(cell);
row.appendChild(cell1);
}
tblBody.appendChild(row);
}
tbl.appendChild(tblBody);
document.body.appendChild(tbl);
tbl.setAttribute("border", "2");
如有帮助,欢迎采纳哈~
您首先需要提供一下您当前的shoppingList的基本内容,我想应该您的list获取之后,您还需要再进一步获取这个对象的具体值,希望对您有帮助!