JS table generator with buttons

前端 未结 3 1399
自闭症患者
自闭症患者 2021-01-16 02:30

I found this answer on the website and used it as a base for my own code.

I was trying to get the buttons to render within the table but it just came up as text and n

3条回答
  •  爱一瞬间的悲伤
    2021-01-16 02:48

    You need to create a button node and then append it.

    function tableCreate() {
      var body = document.getElementsByTagName("body")[0];
    
      var tbl = document.createElement("table");
      var tblBody = document.createElement("tbody");
    
      for (var j = 0; j <= 10; j++) {
        var row = document.createElement("tr");
    
        for (var i = 0; i <10; i++) {
          var cell = document.createElement("td");
          var button document.createElement("button");
          button.innerHTML = j +"-"+i;
          cell.appendChild(button);
          row.appendChild(cell);
        }
        tblBody.appendChild(row);
      }
      tbl.appendChild(tblBody);
      body.appendChild(tbl);
      tbl.setAttribute("border", "2");
    }
    
    function cell_id(x,y){
      msg('x:'+x+ ' y:'+y)
    }

提交回复
热议问题