在html中创建表的脚本未正确放置

时间:2018-07-02 16:13:45

标签: javascript html

我试图通过删除td和th的行和行来清理我的html表,并使用脚本将其自动化。

我找到了此解决方案here

    function createTable(tableData) {
  var table = document.createElement('table');
  var tableBody = document.createElement('tbody');

  tableData.forEach(function(rowData) {
    var row = document.createElement('tr');

    rowData.forEach(function(cellData) {
      var cell = document.createElement('td');
      cell.appendChild(document.createTextNode(cellData));
      row.appendChild(cell);
    });

    tableBody.appendChild(row);
  });

  table.appendChild(tableBody);
  document.body.appendChild(table);
}

createTable([["row 1, cell 1", "row 1, cell 2"], ["row 2, cell 1", "row 2, cell 2"]]);

现在,在尝试编辑单元格的内容之前,我想通过复制和粘贴确保它在我的文档中可以正常工作,

这是原始文档的小提琴: https://jsfiddle.net/frodomeg/5u6czpfa/

这是小提琴,我试图将新脚本放置在每个div中: https://jsfiddle.net/frodomeg/07dbwmy4/2/

正如您在第二个链接中看到的那样,正在发生的结果是在页面底部而不是每个div中生成输出。老实说,我不知道我在做什么错,因为我对js很陌生。

有什么想法吗? 谢谢

1 个答案:

答案 0 :(得分:0)

  

输出是在页面底部而不是每个div中产生的

您说:

Intent intent = new Intent();
intent.setClassName("com.huawei.systemmanager", "com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity");
startActivity(intent);

…是document.body.appendChild(table); 元素的结尾。

如果要在div内添加它,则需要将其附加到div而不是正文上。