<table id="t">
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>我用javascript怎么写才能在tr里创建一个td,并且可以给这个td的innerhtml赋值。

解决方案 »

  1.   

    var table = document.getElementById('t');var tr = document.createElement('TR');var td = document.createElement('TD);td.innerHTML = 'Hello World~';tr.appendChild(td);table.appendChild(tr);
      

  2.   


    <html>
    <body>
    <table id="t">
        <tr>
         <td>&nbsp;</td>
        </tr>
    </table>
    <script type="text/javascript">
    var table = document.getElementById("t");
    var td = document.createElement("td");
    td.innerHTML = "测试内容";
    table.rows[0].appendChild(td);
    </script>
    </body>
    </html>
      

  3.   


       //删除表格
        function delTable()
        {
            var theTable = document.getElementById("table1");  // 所在表格的ID          
            while(theTable.hasChildNodes())                   // 删除表格
             {     
                theTable.removeChild(theTable.lastChild);
            }      
                        
        }
      
        
        function  insRow()    //  动态创建表格, 插入table,row,cols, 
        {               
            
            var theTable = document.getElementById("tableFujian");        
            var  hidtempValue = document.getElementById("hidTempValue");
            var  hidUrlValue = document.getElementById("hidFujianUrlString");
                            
            if(hidFujianString != "" )
            {
                var  str= hidFujianString.split(",");                                   
        delTable(); //清理数据
                                 
                for( var i=0;i<= str.length; i++ )
               {
                     document.getElementById("table1").style.display = "";
                     var theRow = theTable.insertRow();
                    
                    if( i == 0 )
                    {
                           
                   theCell = theRow.insertCell();
                 theCell.style.backgroundColor = "#cccccc";
                         theCell.height="5px";
                         theCell.width ="300px";     
                 theCell.innerHTML = "名称";
                 theCell.align = "center";
                 theCell.value =i;               var theCell = theRow.insertCell();
                 theCell.style.backgroundColor = "#cccccc";
                 theCell.height="5px";        
                 theCell.width ="200px";    
                 theCell.innerHTML = "操  作";
                 theCell.align = "center";
                 theCell.value = i;
                  
                 }
                 else
                 {
                          theCell = theRow.insertCell();
                          theCell.style.backgroundColor = "#ccccff";                         
                          theCell.innerHTML = i;                                   
                          theCell.align = "center";
                          theCell.value = i;
                        
                          theCell = theRow.insertCell();
                          theCell.style.backgroundColor = "#ccccff";                                    
                          theCell.innerHTML ="名称"; 
                          theCell.align = "center";                      
                          theCell.value = i;                                           
                                                          
                 }
              
             }         
         
       }                       
        
        }
      

  4.   


    --使用jquery,更省事:
    <script src="jQuery.js"></script>
    <input type="button" value='增加一个单元格' onclick="javascript:add();"/>
    <table id="t" > 
      <tr id="tr1"> 
        <td>&nbsp;aa </td> 
      </tr> 
    </table>
    <script>
    var i = 1;
    function add()
    {
          var htmls="<td>我是新加的</td>";
          $("tr").append(htmls); 
    }
    </script> 
      

  5.   

    <body>删除这个单元格的功能怎么做啊?求大家帮助。<a href="javascript:aaa()">add</a><table id="t" border="1">
        <tr>
            <td>&nbsp;1</td>
        </tr>
    </table>
    <script type="text/javascript">function aaa()
    {
    var table = document.getElementById("t");
    var td = document.createElement("td");
    td.innerHTML = "测试内容<a href='javascript:ccc(this)'>delete</a>";
    table.rows[0].appendChild(td);
    }function ccc(r)
    {
    var s=document.getElementById('t');
    //alert(s);
        var lostTab=s.removeChild(s.lastChild); 
    }</script>   
    </body>