吴小懒

导航

 

如果想通过核心DOM方法来完成这个任务,我们的代码可能会很复杂

那么用下面这个方法可能会大大降低创建表格的复杂度

 //创建table

var oTable=document.createElement("table");

oTable.setAttribute("border","1");

oTable.setAttribute("width","100%");

//创建tbody

var oTBody=document.createElement(tbody);

oTable.appendChild(oTBody);

//创建第一行

oTBody.insertRow(0);

oTBody.rows[0].insertCell(0);

oTBody.rows[0].cells[0].appendChild(document.createTextNode("cell  1,1"));

oTBody.rows[0].insertCell(1);

oTBody.rows[0].cells[1].appendChild(document.createTextNode("cell  2,1"));

//创建第二行

oTBody.insertRow(0);

oTBody.roed[1].insertCell(0);

oTBody.rows[1].cells[0].appendChild(document.createTextNode("cell  1,2"));

oTBody.rows[1].insertCell[1];

oTBody.rows[1].cells[1].appendChild(document.createTextNode("cell  2,2"));

//将table添加到文档body部分

document.body.appendChild(oTable);

posted on 2017-11-05 19:21  吴小懒  阅读(144)  评论(0)    收藏  举报