📌  相关文章
📜  使用 javascript 代码示例动态创建 html 表

📅  最后修改于: 2022-03-11 15:02:20.130000             🧑  作者: Mango

代码示例1
function generate_table() {
  // get the reference for the body
  var body = document.getElementsByTagName("body")[0];

  // creates a  element and a  element
  var tbl = document.createElement("table");
  var tblBody = document.createElement("tbody");

  // creating all cells
  for (var i = 0; i < 2; i++) {
    // creates a table row
    var row = document.createElement("tr");

    for (var j = 0; j < 2; j++) {
      // Create a  in the 
element and a text node, make the text // node the contents of the , and put the at // the end of the table row var cell = document.createElement("td"); var cellText = document.createTextNode("cell in row "+i+", column "+j); cell.appendChild(cellText); row.appendChild(cell); } // add the row to the end of the table body tblBody.appendChild(row); } // put the
tbl.appendChild(tblBody); // appends
into body.appendChild(tbl); // sets the border attribute of tbl to 2; tbl.setAttribute("border", "2"); }