日期:2014-05-16  浏览次数:20362 次

如何用javascript在div中生成表格?
<html>
<head> </head>
<body> <div   id= 'testdiv '> </div> </body>
</html>
我想在testdiv中用javascript创建一个表格,请问该怎么写?

------解决方案--------------------
楼上的答案可以,当然也可以更复杂的方法
var table = document.createElement( " <TABLE> </TABLE> ");
var tr = table.insertRow();
var td = tr.insertCell();
然后 td.innerHTML = ' ';进行填充
最后div.appendChild(table)搞定
------解决方案--------------------
var table = document.createElement( " <TABLE> </TABLE> ");
var tr = table.insertRow();
var newCell1 = newRow.insertCell();
newCell1.innerHTML = " <table> <tr> <td nowrap> 1列 </td> </tr> </table> ";//这里填充自己html
var newCell2 = newRow.insertCell();
newCell2.innerHTML = " ";//加第2列
................................
一行添加多列,添加多行也类似,LZ可以做个循环