2011-05-21 19 views
1

如何在2之间创建表格行? 我正在使用下面的代码来创建一个行。使用javascript在2行之间创建表格行?

if (!document.getElementsByTagName) return; 
     tabBody=document.getElementsByTagName("TBODY").item(0); 

     row=document.createElement("TR"); 

     cell1 = document.createElement("TD"); 

     textnode1=document.createTextNode(''); 

     cell1.appendChild(textnode1); 


     row.appendChild(cell1); 

     tabBody.appendChild(row); 


} 

其实我想创建它作为中间行。目前我的表格有2行。

回答

2

的表暴露自己的方法,可以用来修改:

- insertRow 
- deleteRow 

的表行公开了可用于插入新单元的方法: insertCell
Here是一些基本的例子。

+0

伟大的链接!谢谢。 – mtk 2012-07-15 18:26:52

0

尝试使用的insertBefore;通过计算中间行的索引,然后的insertBefore的nextSibling开始得到该行权指标

tabBody.insertBefore(row, tabBody.childNodes[0].nextSibling);