table.insertRow().insertCell().appendChild(newInput);

解决方案 »

  1.   

    你问的错了,行tr是没有名称name属性的,只有ID属性,你根据id属性就能得到这行的对象在table里增加一行的方法<HEAD>
    </HEAD>
    <SCRIPT LANGUAGE="JavaScript">
    <!--global_Flag = 1;
    function save()
    {
    var trd=document.all.tabA.insertRow();
    trd.ID = "newtr_"+global_Flag;
    trd.insertCell().innerHTML="777";
    trd.insertCell().innerHTML="888";
    trd.insertCell().innerHTML="999"; global_Flag++;
    alert("你新增的行的ID:"+trd.ID);
     }
    //-->
    </SCRIPT><BODY><table id=tabA border=1>
    <tr >
    <td nowrap>111</td>
    <td nowrap>222</td>
    <td nowrap>333</td>
    </tr>
    <tr >
    <td nowrap>444</td>
    <td nowrap>555</td>
    <td nowrap>666</td>
    </tr>
    </table>
    <INPUT TYPE="button" value="go" onclick="save();">
    </BODY>
      

  2.   

    function addRow(){
    var tblObj = document.getElementById("table1");
    if(tblObj.rows){
    alert(tblObj.rows.length);
    }else{
    alert('aaa');
    }
    //追加行
    var newRow = tblObj.insertRow();
    newRow.style.display = "";
    var cellNum = tblObj.rows[0].cells.length;//追加列
    for (colIndex = 0; colIndex < cellNum; colIndex++) {
    var newCell = newRow.insertCell();
    initTblCell(newCell);
    }
    }
    function initTblCell(cell){
    var lastCell = document.getElementById("table1").rows[0].cells[cell.cellIndex];
    cell.innerHTML = lastCell.innerHTML;
    alert(cell.innerHTML);
    if (cell.children != null && cell.children.length > 0) {
    for(childIndex = 0; childIndex < cell.children.length; childIndex++) {
    var child = cell.children[childIndex];
    switch(child.type) {
    case "text":
    child.value = "";
    break;
    case "checkbox":
    child.value = "";
    child.checked = false;
    break;
    }
    }
    }
    cell.className = lastCell.className;
    cell.align = lastCell.align;
    cell.height = lastCell.height;
    }
    function buttonFun(){
    var obj= document.getElementsByName("text4");
    for(i=1;i<obj.length;i++){
    alert(obj[i].value);
    }}
    function dellRow(){
    var obj= document.getElementById("table1");
    var objRow=obj.rows.length-1;
    alert(objRow);
    if(objRow != 0){
    obj.deleteRow(objRow);
    }}  <table id="table1" name="table1" width="300">
            <tr style="display:none">
        <td width="100">
        <input type="button" name="testBtn4" value="button" onclick=""/>
        </td>
        <td width="100">
        <input type="text" name="text4"  id="text4"/>
        </td>
        <td width="100">
        123
        </td>
        </tr>
        
        <tr>
        <td width="100">
        <input type="button" name="testBtn4" value="button" onclick="toExcel_Page()"/>
        </td>
        <td width="100">
        <input type="text" name="text4"  id="text4"/>
        </td>
        <td width="100">
        123
        </td>
        </tr>
        </table>
        <input type="button" name="testAdd" id="testAdd" value="AddRow" onclick="addRow()"/>
        <input type="button" name="testAdd" id="testAdd" value="DellRow" onclick="dellRow()"/>
        <input type="button" name="testAdd2" id="testAdd2" value="GetTextValue" onclick="buttonFun()"/>
      

  3.   

    动态添加行如果是 copy上一行的话,他的name和上一行一样,上面的例子就是这样,
    如果是.insertCell().innerHTML="<tr id="" name=""> ...<tr>",这样添加的话,id, name就可以直接加,不过name没什么用
      

  4.   

    谢谢 fengruzhuo 和 mingxuan3000 .问题已经解决. 又学到了一些新的用法.是以前没接触到的.  初来CSDN.不晓得这分是怎么给你们啊?
      

  5.   

    这样进
       CSDN社区起始点-->我的帖子-->管理-->给分结贴记住解决问题了要给分结贴哦,否则人品会降,呵呵
      

  6.   

    TO :mingxuan3000(铭轩)   .insertCell().innerHTML=  "  <tr  id=  "  "  name=  "  "  >  ...  <tr  >  "
    用这种方法插入是能实现名称的自定,可是一个表格如果有三行.这种方法在第一格插入以后就会转到下行去在插. 不知道你是怎么实现的?
      

  7.   

    还是把我的贴上来,我是这样写的.插进去有问题,你看看<script language="javascript">
    var i = 0;
      function addTR()
      {
        i++;
       var iTab = document.getElementById("iTab");
    var iTr = iTab.insertRow(); var cellNum = iTab.rows[0].cells.length; for (colIndex = 0; colIndex < cellNum; colIndex++) 
    {
    var newCell = iTr.insertCell();
            //问题就这里啦,控制不了.
    newCell.innerHTML="<tr id='tr'"+i+"><td><input name=chk_1 type=checkbox id=chk_1 value=\"1\" /></td><td>-->"+i+"<--</td></tr>";
           //全插进一格里去啦.如果分开写,比如说在第一格插什么,在第二格在插什么,这个应该可以实现.但有没有简洁的.
    }

      }
    </script>
    <table width="200" height="54" border="1" align="center" id="iTab">
      <tr id="tr0">
        <td>
          <input name=chk_1 type=checkbox id=chk_1 value="1" />
        </td>
        <td>0</td>
      </tr>
    </table><input type="button" name="Submit" value="增加" onclick="addTR()" />
      

  8.   

    简洁的?
    copy上一行,上面3的例子就是这样,