从HTML表中隐藏

时间:2016-10-19 10:40:44

标签: javascript html html-table

我正在尝试通过JavaScript创建动态html表。我需要隐藏teamid td(cell1)。需要将显示设置为无。我怎么能这样做?

function generate_table() 
{
    var body = document.getElementsByTagName("body")[0];
    var tbl = document.createElement("table");
    var tblBody = document.createElement("tbody");
    var teamrecord = "test";
    for (var i = 0; i <  teamrecord.length; i++) {
        var row = document.createElement("tr");

        var cell = document.createElement("td");
        var cell1 = document.createElement("td");
        var cell2 = document.createElement("td");

        var cellText = document.createTextNode("teamrecord");
        var cellId = document.createTextNode("teamid");
        var radio = document.createElement("INPUT");
        radio.setAttribute("type", "radio");
        radio.setAttribute("name", "radio");

        cell.appendChild(cellText);
        cell1.appendChild(cellId);
        cell2.appendChild(radio);

        row.appendChild(cell);
        row.appendChild(cell1);
        row.appendChild(cell2);

        tblBody.appendChild(row);
    }

    tbl.appendChild(tblBody);
    body.appendChild(tbl);
}

我使用以下代码,但它不起作用

cell1.setAttribute("display","none");

4 个答案:

答案 0 :(得分:3)

就像这样:

~baduser/

答案 1 :(得分:0)

只需将所需单元格的样式display设置为none

cell1.style.display = 'none';添加到您的代码中。

以下工作代码:

function generate_table() {
  var body = document.getElementsByTagName("body")[0];
  var tbl = document.createElement("table");
  var tblBody = document.createElement("tbody");
  var teamrecord = "test";

  for (var i = 0; i < teamrecord.length; i++) {
    var row = document.createElement("tr");

    var cell = document.createElement("td");
    var cell1 = document.createElement("td");
    var cell2 = document.createElement("td");

    var cellText = document.createTextNode("teamrecord");
    var cellId = document.createTextNode("teamid");
    var radio = document.createElement("INPUT");
    radio.setAttribute("type", "radio");
    radio.setAttribute("name", "radio");


    cell.appendChild(cellText);
    cell1.appendChild(cellId);
    cell2.appendChild(radio);

    cell1.style.display = 'none';

    row.appendChild(cell);
    row.appendChild(cell1);
    row.appendChild(cell2);

    tblBody.appendChild(row);
  }
  tbl.appendChild(tblBody);
  body.appendChild(tbl);

}
generate_table();

答案 2 :(得分:0)

您需要将其定义为这样的样式:

cell1.style.display = 'none';

REF: http://www.w3schools.com/jsref/prop_style_display.asp

答案 3 :(得分:0)

尝试使用cell1.style.visibility = "hidden"