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

表格插入行的一点小问题
最近喜欢在Firefox下做东西,方便调试脚本。今天做插入行  
var   newRow=document.getElementById( "mytable ").insertRow(insertRowIndex);  

这样应该算建立一行了吧。然后我直接用newRow.innerHTML=getCol();

getCol()   返回的是 " <td> aa </td> <td> bb </td> "
等这样的东西,这个在firefox下通过,成功插入了行。

但是在IE下报错   :   未知的运行时错误。

ie下可能是在insertRow后就要立马insertCell建立 <td> 才可以,但是因为我有很多地方要用到插入行,而数据都是可以从   getCol()方法取得的,如果每次都要insertCell的话,要写很多代码,这不划算。
所以请问我如何才能建立行,并从getCol()直接拿到 <td>   ?

------解决方案--------------------
可以用dom做
var d = document.createElement( 'table ');
返回i行j列可以这样用
d.cells[i].rows[j]
------解决方案--------------------
可以这样
document.getElementById( "mytable ").parentElement.innerHTML = " <table> <tr> " + getCol() + " </tr> </table> ";

To change the contents of the table, tFoot, tHead, and tr elements, use the table object model described in How to Build Tables Dynamically. For example, use the rowIndex property or the rows collection to retrieve a reference to a specific table row. You can add or delete rows using the insertRow and deleteRow methods. To retrieve a reference to a specific cell, use the cellIndex property or the cells collection. You can add or delete rows using the insertCell and deleteCell methods. To change the content of a particular cell, use the innerHTML property.