日期:2014-05-17  浏览次数:20481 次

javascript中的动态创建元素~~~~求解!!
<html>
<head>
<titile></title>
<script type="text/javascript">
function loadData(){

  var data={"百度":"http://www.baidu.com","新浪":"http://www.sina.com","雅虎":"http://www.yahoo.com"};
  tableLinks=document.getElementById("tableLinks");
  
  for(var key in data){
  var tr=document.createElement("tr");
  var value=data[key];  
   
  var td1=document.createElement("td");
  td1.innerText=key;
  tr.appendChild(td1);
   
  var td2=document.createElement("td");
  td2.innerHTML="<a href='"+value+"'>"+value+"</a>";
  tr.appendChild(td2);
   
  tableLinks.appendChild(tr);
}
</script>
</head>
<body>
<table id="tableLinks"></table>
<input type="button" value="添加数据" onclick="loadData();" />
</body>
</html>

以上是源码.在IE中数据显示不出来,我用debugbar看了一下生成的代码如下:
<TABLE id=tableLinks>
<TBODY></TBODY>
<TR>
<TD>百度</TD>
<TD><A href="http://www.baidu.com/">http://www.baidu.com</A></TD></TR>
<TR>
<TD>新浪</TD>
<TD><A href="http://www.sina.com/">http://www.sina.com</A></TD></TR>
<TR>
<TD>雅虎</TD>
<TD><A href="http://www.yahoo.com/">http://www.yahoo.com</A></TD></TR></TABLE><INPUT onclick=loadData(); value=添加网站列表 type=button> </BODY></HTML>

这个怎么解决?数据已经添加到id=tableLinks的表格中了,可是出现了tbody这个标签,求大神解决!!

------解决方案--------------------
表格结构体:
<table>
<thead>
</thead>
<tbody>
</tbody>
</tfoot>
</tfoot>
</table>

其中: thead, tfoot 只能有一个. 如果有多个,那么其他的忽略,并视为tbody内容。

tbody可以有多个.以tBodies[index] 区别多个tbody.

通常, thead,tfoot, tbody 可以省略。但是:

在js应用时, tbody不能省。

tableLinks=document.getElementById("tableLinks");
tableLinks.tBodies[0].....