var data=[
{name:”aa”,age:16,gender:”male”},
{name:”bb”,age:18,gender:”female”},
{name:”cc”,age:19,gender:”male”},
{name:”dd”,age:20,gender:”female”},
];
// 创建表格大概样式
var table=document.createElement(“table”);
var tbody=document.createElement(“tbody”);
var thead=document.createElement(“thead”);
table.appendChild(tbody);
table.appendChild(thead);
var trHead=document.createElement(“tr”);
thead.appendChild(trHead);
// 创建表头
for(var k in data[0]){
var th=document.createElement(“th”);
th.appendChild(document.createTextNode(k));
trHead.appendChild(th)
}
//创建每行的tr和td
for(var i=0;i<data.length;i++){
var tr=document.createElement(“tr”);
for( var k in data[i]){
var td=document.createElement(“td”);
td.appendChild(document.createTextNode(data[i][k]));
tr.appendChild(td);
td.width=”100″;
td.height=”40″;
}
tr.align=”center”;
tbody.appendChild(tr);
}
table.border=”1″;
//加到页面中
document.body.appendChild(table);