javascript 动态写表格怎么没出来,帮看看,谢谢!

时间:2022-03-29 14:06:09

<html>
  <script type="text/javascript">
    function createElement()
    {
     var tb=document.createElement("table");
     tb.setAttribute("boder","1");
     tb.setAttribute("height","100");
     tb.setAttribute("width","500");
     tb.setAttribute("cellpadding","10");
     tb.setAttribute("cellspacing","20");
     tb.setAttribute("bgcolor","red");
     var tr=document.createElement("tr");
     var td=document.createElement("td");
     var txt=document.createTextNode("测试表格");
     //alert("节点类型"+tr.nodeType+"节点名称"+tr.nodeName);
     td.appendChild(txt);
     tr.appendChild(td);
     tb.appendChild(tr);
     /*for(var i=0;i<5;i++)
     {
      
       for(var j=0;j<6;j++)
       {
        
       }
     }
        */
     document.body.appendChild(tb);
     
    }
  </script>
<body onload="createElement()">
 
</body>
</html>

10 个解决方案

#1


<html>
  <script type="text/javascript">
    function createElement()
    {
     var tb=document.createElement("table");
     tb.setAttribute("boder","1");
     tb.setAttribute("height","100");
     tb.setAttribute("width","500");
     tb.setAttribute("cellpadding","10");
     tb.setAttribute("cellspacing","20");
     tb.setAttribute("bgcolor","red");
     document.body.appendChild(tb);
     var tr=document.createElement("tr");
     tb.appendChild(tr);
     var td=document.createElement("td");
     tr.appendChild(td);
     var txt=document.createTextNode("测试表格");
     //alert("节点类型"+tr.nodeType+"节点名称"+tr.nodeName);
     td.appendChild(txt);
     /*for(var i=0;i<5;i++)
     {
      
       for(var j=0;j<6;j++)
       {
        
       }
     }
        */
     
    }
  </script>
<body onload="createElement()">
 
</body>
</html>

#2


试了一下在firefox可以用,在ie中不可以用.看不出来为什么

#3


IE中用的不是document.createElement这样子的方法。。不一样的。。要看标准才知道。。我做东东只做IE下的。。

#4


<html>
  <script type="text/javascript">
    function createElement()
    {
     var tb=document.createElement("table");
     tb.setAttribute("boder","1");
     tb.setAttribute("height","100");
     tb.setAttribute("width","500");
     tb.setAttribute("cellpadding","10");
     tb.setAttribute("cellspacing","20");
     tb.setAttribute("bgcolor","red");
     tr=tb.insertRow();
     td=tr.insertCell();
     txt=document.createTextNode("测试表格");
     td.appendChild(txt);
     document.body.appendChild(tb);
    }
  </script>
<body onload="createElement()">
 
</body>
</html>

#5


难道 IE 下只能insertRow()  insertCell() ?

#6


引用 4 楼 changke18 的回复:
HTML code<html>
  <script type="text/javascript">
    function createElement()
    {
     var tb=document.createElement("table");
     tb.setAttribute("boder","1");
     tb.setAttribute("height","100");
     tb.setAttribute("width","500");
     tb.setAttribute("cellpadding","10");
     tb.setAttribute("cellspacing","20");
     tb.setAttribute("bgcolor","red");
     tr=tb.insertRow();


这种方式可以显示出输入结点的文字。但是像table的border="1",和bgcolor="red"都没有正确的显示啊。
不知哪位仁兄可以帮忙讲解一下。

#7


不好意思,border  写成boder 

#8


有的属性没生效,cellspacing  cellpadding ??帮忙解释下?

#9


js动态创建table,里面要套个TBODY才能显示出来

#10


<html>
  <script type="text/javascript">
    function createElement()
    {
     var tb=document.createElement("table");
     tb.setAttribute("boder","1");
     tb.setAttribute("height","100");
     tb.setAttribute("width","500");
     tb.setAttribute("cellpadding","10");
     tb.setAttribute("cellspacing","20");
     tb.setAttribute("bgcolor","red");
     tr=tb.insertRow();
     td=tr.insertCell();
     txt=document.createTextNode("测试表格");
     td.appendChild(txt);
     document.body.appendChild(tb);
    }
  </script>
<body onload="createElement()">
 
</body>
</html>

#1


<html>
  <script type="text/javascript">
    function createElement()
    {
     var tb=document.createElement("table");
     tb.setAttribute("boder","1");
     tb.setAttribute("height","100");
     tb.setAttribute("width","500");
     tb.setAttribute("cellpadding","10");
     tb.setAttribute("cellspacing","20");
     tb.setAttribute("bgcolor","red");
     document.body.appendChild(tb);
     var tr=document.createElement("tr");
     tb.appendChild(tr);
     var td=document.createElement("td");
     tr.appendChild(td);
     var txt=document.createTextNode("测试表格");
     //alert("节点类型"+tr.nodeType+"节点名称"+tr.nodeName);
     td.appendChild(txt);
     /*for(var i=0;i<5;i++)
     {
      
       for(var j=0;j<6;j++)
       {
        
       }
     }
        */
     
    }
  </script>
<body onload="createElement()">
 
</body>
</html>

#2


试了一下在firefox可以用,在ie中不可以用.看不出来为什么

#3


IE中用的不是document.createElement这样子的方法。。不一样的。。要看标准才知道。。我做东东只做IE下的。。

#4


<html>
  <script type="text/javascript">
    function createElement()
    {
     var tb=document.createElement("table");
     tb.setAttribute("boder","1");
     tb.setAttribute("height","100");
     tb.setAttribute("width","500");
     tb.setAttribute("cellpadding","10");
     tb.setAttribute("cellspacing","20");
     tb.setAttribute("bgcolor","red");
     tr=tb.insertRow();
     td=tr.insertCell();
     txt=document.createTextNode("测试表格");
     td.appendChild(txt);
     document.body.appendChild(tb);
    }
  </script>
<body onload="createElement()">
 
</body>
</html>

#5


难道 IE 下只能insertRow()  insertCell() ?

#6


引用 4 楼 changke18 的回复:
HTML code<html>
  <script type="text/javascript">
    function createElement()
    {
     var tb=document.createElement("table");
     tb.setAttribute("boder","1");
     tb.setAttribute("height","100");
     tb.setAttribute("width","500");
     tb.setAttribute("cellpadding","10");
     tb.setAttribute("cellspacing","20");
     tb.setAttribute("bgcolor","red");
     tr=tb.insertRow();


这种方式可以显示出输入结点的文字。但是像table的border="1",和bgcolor="red"都没有正确的显示啊。
不知哪位仁兄可以帮忙讲解一下。

#7


不好意思,border  写成boder 

#8


有的属性没生效,cellspacing  cellpadding ??帮忙解释下?

#9


js动态创建table,里面要套个TBODY才能显示出来

#10


<html>
  <script type="text/javascript">
    function createElement()
    {
     var tb=document.createElement("table");
     tb.setAttribute("boder","1");
     tb.setAttribute("height","100");
     tb.setAttribute("width","500");
     tb.setAttribute("cellpadding","10");
     tb.setAttribute("cellspacing","20");
     tb.setAttribute("bgcolor","red");
     tr=tb.insertRow();
     td=tr.insertCell();
     txt=document.createTextNode("测试表格");
     td.appendChild(txt);
     document.body.appendChild(tb);
    }
  </script>
<body onload="createElement()">
 
</body>
</html>