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()

解决方案 »

  1.   

    <HTML>
    <HEAD>
    <style>
    table.description {
        border: 2px solid black;
        border-collapse: collapse;
        font-size: x-small;
        border-color:#2E8B57;}
    .aqua {
        background-color: #DDDDDD;
        color: black;
        white-space: nowrap;
        border: 1px solid black;
        border-color:#666666;
    }
    .white {
        background-color: white;
        color: black;
        white-space: nowrap;
        border: 1px solid black;
        border-color:#666666;
    }
    .input_char_l {
    ime-mode:disabled;
    }
    </style>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function addLine(){
    var obj=document.getElementById("table1")
    var index=obj.childNodes[0].childNodes.length;
    var objTr=obj.childNodes[0].childNodes[1].cloneNode(true);
    obj.childNodes[0].appendChild(objTr);
    for(var i=0;i<objTr.childNodes.length;i++)
    {
    objTr.childNodes[i].innerHTML=objTr.childNodes[i].innerHTML.replace(/1/g,index);
    }
    }
    //-->
    </SCRIPT>
    </HEAD>
    <BODY> <table class="description" cellpadding="3" border="1" id="table1">
    <tr>
    <th class="aqua">
    a
    </th>
    <th class="aqua">
    b
    </th>
    <th class="aqua">
    c
    </th>
    <th class="aqua">
    d
    </th>
    <th class="aqua">
    e
    </th>
    <th class="aqua">
    f
    </th>
    </tr>
    <tr>
    <td  class="white"align = "center">
    1
    </td>
    <td  class="white"align = "center">
    1
    </td>
    <td  class="white"align = "center">
    1
    </td>
    <td  class="white"align = "center">
    1
    </td>
    <td  class="white"align = "center">
    1
    </td>
    <th class="white" align="center">
    <input type="text" name="TextA" value="1" class="input_char_l">
    </th>
    </tr>
    <tr>
    <td  class="white"align = "center">
    2
    </td>
    <td  class="white"align = "center">
    2
    </td>
    <td  class="white"align = "center">
    2
    </td>
    <td  class="white"align = "center">
    2
    </td>
    <td  class="white"align = "center">
    2
    </td>
    <th class="white" align="center">
    <input type="text" name="TextA" value="2" class="input_char_l">
    </th>
    </tr>
    </table>
    <input type='button' value="Add Line" onclick="addLine()">
    </BODY>
    </HTML>