把要增加的行设置成隐藏,display = 'none' 当要显示时 display = ''可以达到同样效果

解决方案 »

  1.   

    var tr = objTable.insertRow(index); //index=0 , 在表头增加, index=-1, 在表尾增加,你这里应该index应该是1
      

  2.   

    可以通过a找到 b 然后 insertBefore
    像这样
    <script>
    function insert(id){
    var otr = document.getElementById(id), ntr = otr.nextSibling, tr = document.createElement('tr'), td = document.createElement('td');
    td.innerHTML = '<input type="text" name="user" />';
    tr.appendChild(td);
    ntr ? ntr.parentNode.insertBefore(tr, ntr) : otr.parentNode.appendChild(tr);
    }
    </script>
    <table>
    <tr id="a"><td>a</td></tr>
    <tr><td>b</td></tr>
    </table>
    <input type="button" onclick="insert('a')" value="插入" />
      

  3.   

    var oTable = document.getElementById("tbInfo");
    var oTr = oTable.insertRow(-1);//在最后一行后面插入
    var oCell = oTr.insertCell();  //插入表格 
      

  4.   

    如:
    var newRow = table.insertRow(1)
    参考:
    insertRow Method  Internet Development Index --------------------------------------------------------------------------------Creates a new row (tr) in the table, and adds the row to the rows collection. SyntaxoTR = object.insertRow( [iIndex])
    ParametersiIndex Optional. Integer that specifies where to insert the row in the rows collection. The default value is -1, which appends the new row to the end of the rows collection. Return ValueReturns the tr element object if successful, or null otherwise. ResIf you insert a row in a tFoot, tBody, or tHead, you also need to add the row to the rows collection for the table. If you insert a row in the table, you also need to add the row to the rows collection for the tBody. If you specify an index, the index should be relative to the rows collection for the element that first contains the tr. For example, if you call this method for a tBody, you must specify an index value relative to the rows collection that is on the tBody, not the table. The preferred technique for inserting a row is to add the row at the end of the rows collection. It is faster to add a row at the end of a table than somewhere in the middle. To add a row at the end of the collection, specify the -1 value, or the length of the rows collection minus 1.ExampleThis example uses the insertRow method to add a row to the table. myNewRow = document.all.myTable.insertRow()
      

  5.   

    例子:在选中行之前添加新行<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    <META NAME="Generator" CONTENT="EditPlus">
    <META NAME="Author" CONTENT="">
    <META NAME="Keywords" CONTENT="">
    <META NAME="Description" CONTENT="">
    </HEAD>
    <script>
    var oldRow;
    //选中行
    function changeTrColor(row){
    if(oldRow){
    oldRow.style.backgroundColor="";
    }
    row.style.backgroundColor="red";
    oldRow=row;
    }
    //在选中行之前插入新行
    function addRow(){
    if(!oldRow){
    alert("请选择行!");
    return false;
    }
    var table=document.getElementById("tab");
    var n=oldRow.rowIndex;
    var row=table.insertRow(n);
    addEvent(row,"click",function(){changeTrColor(row);});
    var cell=row.insertCell();
    cell.appendChild(document.createTextNode("new row"+n));
    }
    //添加事件
    function addEvent(object,eventType,eventHandler){
    if(object.addEventListener){//2级DOM
    object.addEventListener(eventType,eventHandler,true);
    }else if(document.attachEvent){//IE5+
    object.attachEvent("on"+eventType,eventHandler);
    }else{//IE4
    object["on"+eventType]=eventHandler;
    }
    }
    </script>
    <BODY>
    <table id="tab" border="1">
    <tr onclick="changeTrColor(this);">
    <td>dsaf</td>
    </tr>
    <tr onclick="changeTrColor(this);">
    <td>dsaf</td>
    </tr>
    <tr onclick="changeTrColor(this);">
    <td>dsaf</td>
    </tr>
    </table>
    <input type="button" value="new row" onclick="addRow();">
    </BODY>
    </HTML>
      

  6.   

    var ob=document.getElementById('tablename');
    var tr=ob.insertRow(1); 
    这样就OK啦