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

js动态生成表格
直接上代码:
<html> 
<head> 
<title> </title> 
<script   type= "text/javascript"> 
    function TestSecond() 
    { 
        var divt=document.getElementById("div1"); //显示DIV
        var tablet=document.createElement("table"); //创建表
        tablet.border=1;        
		var textRow=2;	//行数
        var textCell=3;	//列数
        var num = 0;
        for(var i=0;i<textRow;i++) 
        {  
            var row=tablet.insertRow(num); 
            for(var j=0;j<textCell;j++) 
            { 
                var cell=row.insertCell(num); 
                cell.innerHTML="hello"; 
                cell.width=150; 
                cell.height = 20; 
            } 
        } 
        num = num + 1; 
            
      divt.appendChild(tablet); 
	 
    } 

//根据tr的ID删除一行(未被调用,备用)
function deleteOneTr(trId)
{
	var theTab = document.getElementById("oneTab")//根据表格ID得表格
	var row=document.getElementById(trId); 
	var index=row.rowIndex;
	theTab.deleteRow(index);
}

</script> 
</head> 
<body> 
<form id="Form1" method="post" runat="server"> 
<table align="center"> 
             
                <tr> 
                    <td align="center" colspan="2"><input type="button" value="生成表格" onClick="javascript:TestSecond();"/></td> 
                </tr> 
            </table> 
<div id="div1" style="position:absolute;"></div> 
     </form> 
</body> 
</html>