在网上看到一段代码:var obtn = document.createElement("input");
obtn.setAttribute("type", "text");
obtn.setAttribute("id", "txt");
document.body.appendChild(obtn);
实验了一下,可以添加控件,如果想把控件加入到表格中怎么写?document.?还是其他的方法?
这个代码是添加控件的,那么我想删除一个控件,怎么写?本人小小菜鸟一只,各位达人多多指教。

解决方案 »

  1.   

    var obtn = document.createElement("input"); 
    obtn.setAttribute("type", "text"); 
    obtn.setAttribute("id", "txt"); 
    document.body.appendChild(obtn);
    //红色部分是你要添加到哪个容器,如果想添加到表格
    <table id="myTab"><tr><td></td></tr></table>document.getElementById("myTab").rows[0].cells[0].appendChild(obtn);删除表格类似
    var obtn = document.getElementById("txt");
    document.body.removeChild(obtn);
      

  2.   

    删除控件类似 
    var obtn = document.getElementById("txt"); 
    document.body.removeChild(obtn);
      

  3.   

    document.all['控件名'].removeNode(true);
      

  4.   

    <script src="js/jquery-1.3.2.js"></script>
    <script>
    function add(){
      var obtn = document.createElement("input"); 
      obtn.setAttribute("type", "text"); 
      obtn.setAttribute("id", "txt"); 
      document.getElementById("td").appendChild(obtn); 
    }
    function del(){
      var obj = document.getElementById("txt");
      if(obj) obj.removeNode(true);
    }
    </script>
    <input type="button" value="add" onclick="add()"><input type="button" value="del" onclick="del()">
    <table border="1">
    <tr><td id="td"><td></tr>
    </table>
      

  5.   


    <script>
    function add(){
      var obtn = document.createElement("input"); 
      obtn.setAttribute("type", "text"); 
      obtn.setAttribute("id", "txt"); 
      document.getElementById("td").appendChild(obtn); 
    }
    function del(){
      var obj = document.getElementById("txt");
      if(obj) obj.removeNode(true);
    }
    </script>
    <input type="button" value="add" onclick="add()"><input type="button" value="del" onclick="del()">
    <table border="1">
    <tr><td id="td"><td></tr>
    </table>
      

  6.   

    我可以用后台页的思维吗?添加一个控件到一个新row,然后添加row到table?我想要的是这种效果。方便控制样式。