带按钮的JS表生成器 [英] JS table generator with buttons

查看:75
本文介绍了带按钮的JS表生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网站上找到了此答案,并将其用作我自己的代码的基础

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 not rendering as a element on the page like I wanted too.

以下是此处使用的代码:

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 btn = "<button>" + j +"-"+i+"</button>"
      var cellText = document.createTextNode(btn);

      cell.appendChild(cellText);
      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)
}


推荐答案

createTextNode()顾名思义,创建一个文本节点。

createTextNode() creates a text node, as the name says. If you have markup in it, it will be shown literally.

您应该已经创建了一个 button 节点,并将其文本设置为

You should have created a button node, and set its text to the variables like this example below.

    for (var i = 0; i <10; i++) {
      var cell = document.createElement("td");
      var btn = document.createElement("button");
      btn.innerText = j + "-" + i;
      cell.appendChild(btn);
      row.appendChild(cell);
    }

这篇关于带按钮的JS表生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆