<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tbody id="ob">
    <tr>
      <td valign="top">AAAA</td>
    </tr>
    <tr>
      <td valign="top">BBBB</td>
    </tr>
  </tbody>
  <tbody id="nb"></tbody>
  <tr>
    <td height="25"><input type="button" name="add" id="add" value="添加" onclick="add();" />
      &nbsp;&nbsp;
      <input type="button" name="del" id="del" value="删除" onclick="del();" /></td>
  </tr>
</table>
<script>
function $(id){
return document.getElementById(id);
}
function add(){
for(i=0;i<$("ob").rows.length;i++){
n=$("ob").rows[i].cloneNode(true);
$("nb").appendChild(n);
}
}
function del(){
$("nb").removeChild($("nb").childNodes.length);
}
</script>为什么添加可以 删除出错呢?

解决方案 »

  1.   

    $(this).remove();
    XX.find(".className").remove();
    试试!!
      

  2.   

    $("nb").removeChild($("nb").childNodes.length-1);
      

  3.   

    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tbody id="ob">
        <tr>
          <td valign="top">AAAA</td>
        </tr>
        <tr>
          <td valign="top">BBBB</td>
        </tr>
      </tbody>
      <tbody id="nb"></tbody>
      <tr>
        <td height="25"><input type="button" name="add" id="add" value="添加" onclick="add();" />
          &nbsp;&nbsp;
          <input type="button" name="del" id="del" value="删除" onclick="del();" /></td>
      </tr>
    </table>
    <script>
    function $(id){
        return document.getElementById(id);
    }
    function add(){
        for(i=0;i<$("ob").rows.length;i++){
            n=$("ob").rows[i].cloneNode(true);
            $("nb").appendChild(n);
        }
    }
    function del(){

        $("nb").removeChild($("nb").childNodes[$("nb").childNodes.length - 1]);
        //removeChild是删除节点对象,不是索引,deleteRow才是索引
    }
    </script>
      

  4.   

    <table id="tb" width="100%" border="0" cellspacing="0" cellpadding="0">
      <tbody id="ob">
        <tr>
          <td valign="top">AAAA</td>
        </tr>
        <tr>
          <td valign="top">BBBB</td>
        </tr>
      </tbody>
      <tbody id="nb"></tbody>
      <tr>
        <td height="25"><input type="button" name="add" id="add" value="添加" onclick="add();" />
          &nbsp;&nbsp;
          <input type="button" name="del" id="del" value="删除" onclick="del();" /></td>
      </tr>
    </table>
    <script>
    function $(id){
        return document.getElementById(id);
    }
    function add(){
        for(i=0;i<$("ob").rows.length;i++){
            n=$("ob").rows[i].cloneNode(true);
            $("nb").appendChild(n);
        }
    }
    function del(){
        if(($("nb").childNodes.length)>1) //做个判断,保留初始的AAA,BBB
        {
    $("nb").removeChild($("nb").lastChild);//前面添加几个TR,后面就删除几个!
     $("nb").removeChild($("nb").lastChild);
     }
    }
    </script>