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

我写的动态添加表格的程序,但不能预期功能,请各位大虾指教
html程序:

<!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   HTML   4.01   Transitional//EN "   "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd ">  
<HTML> <HEAD> <TITLE> creat   my   table </TITLE>  
<META   http-equiv=Content-Type   content= "text/html;   charset=gb2312 ">  
<script   src= "script/dynTable.js ">  
</script>
</head>
<body   onload= "makeTable(); ">
<table   id= "myTable "   border= "1 ">
<tbody>
<tr>
<td> one </td>
<td> two </td>
<td> three </td>
<td> four </td>
</tr>
</tbody>
</table>
<body>
</html>

js文件放在同目录下的script文件下:

function   makeTable()
{
        var   myTable=document.getElementById( "myTable ");
       
        var   tBody=document.creatElement( "tbody ");
myTable.appendChild(tBody);

var   myTr=document.creatElement( "tr ");
tBody.appendChild(myTr);

var   myTd;
for   (var   i=0;   i <4;   i++)
{
myTd=document.creatElement( "td ");
myTd.innerText= "第 "+i+ "列 ";
myTr.appendChild(myTd);
}

document.body.appendChild(myTable);
}

------解决方案--------------------
function makeTable()
{
var myTable=document.getElementById( "myTable ");

var myTBody=document.createElement( "tbody ");
myTable.appendChild(myTBody);

var myTr=document.createElement( "tr ");
myTBody.appendChild(myTr);

var myTd;
for (var i=0; i <4; i++)
{
myTd=document.createElement( "td ");
myTd.innerHTML= "第 "+i+ "列 ";
myTr.appendChild(myTd);
}

//document.body.appendChild(myTable);
}
不知道楼主是否需要这样的效果?楼主的拼写错漏甚多,请自行检查。
------解决方案--------------------
把最后一句去掉document.body.appendChild(myTable); myTable已经是存在的对象了。
creatElement改成createElement