在使用javascript创建表的问题

Problem in creating table using javascript

本文关键字:问题 创建 javascript      更新时间:2023-09-26

嗨,我正在创建一个div内的表,但它没有显示在网页上。我的html代码是

 <div id="right">
  <h2 id="right1">hello !</h2>
  <div id="previewWin">
  </div>
  <div id="welcome">
  </div>
</div>

CSS像:

#right {
    float:right;
  width: 490px;
 padding-right: 110px;
      padding-top: 301px;
    }
    #welcome {
  margin-right: 20px;
}
#previewWin {
background-color: #decea2;
width: 150px;
height: 200px;
font: 1.0em arial, helvetica, sans-serif, larger, bold;
padding: 5px;
position: absolute;
visibility: hidden;
border: 1px  solid ;
overflow: hidden;
}
#previewWin h1, #previewWin h2 {
    font-size: 1.0em;
}

PreviewWin是一个隐藏div:

Javascript代码:

function CreateInterface(){
var table=document.createElement("table");
var tr = document.createElement("tr");
table.appendChild(tr);
var td = document.createElement("td");
tr.appendChild(td);
document.getElementById("welcome").appendChild(table);

}

表现在显示在浏览器中…请帮帮我……这个CreateInterface函数触发一个onclick事件…提前感谢

由于您没有对表格应用任何样式,并且表格为空,因此没有任何内容可供您查看。

如果你想看到没有任何内容的表格,给它一些边框和填充。

您必须在<table>中有一个<tbody>,以便您的代码在Internet Explorer中工作:

var table = document.createElement("table");
var tbody = document.createElement("tbody");
var tr = document.createElement("tr");
table.appendChild(tbody);
tbody.appendChild(tr);

尝试创建tbody,使用它,然后将此tbody附加到table

添加一些内容,你的表格应该是可见的。

function CreateInterface()
{
    var table=document.createElement("table");
    var tr = document.createElement("tr");
    table.appendChild(tr);
    var td = document.createElement("td");
    td.innerHTML = "Hello World";
    tr.appendChild(td);
    document.getElementById("welcome").appendChild(table);
}