创建一个表
var table=document.createElement("table");
table.border=1;
table.width="100%";
创建tbody
var tbody=document.createElement("tbody");
table.appendChild(tbody);
创建第一行
tbody.insertRow(0);
tbody.rows[0].insertCell(0);
tbody.rows[0].cells[0].appendChild(document.createTextNode("cell 1,1"));
tbody.rows[0].insertCell(1);
tbody.rows[0].cells[1].appendChild(document.createTextNode("cell 2,1"));
创建第二行
tbody.insertRow(1);
tbody.rows[1].insertCell(0);
tbody.rows[1].cells[0].appendChild(document.createTextNode("cell 1,2"));
tbody.rows[1].insertCell(1);
tbody.rows[1].cells[1].appendChild(document.createTextNode("cell 2,2"));
将表格添加到文档主体中
document.body.appendChild(table);
DOM表格操作
最新推荐文章于 2021-02-08 18:39:58 发布