为什么无法使用Javascript生成动态表?

时间:2018-07-12 12:54:21

标签: javascript html

我想用Javascript生成动态表,想生成如下表:

<table>
  <tbody>
  <tr>
    <th>Owner</th>
    <th>Number</th>
  </tr>
  <tr>
    <td>aaa</td>
    <td> 3  </td>
  </tr>
 ...
</tbody>
</table>

然后我编写了一个脚本来生成表,但是我无法获取正确的表,我不知道哪里出了问题,有人可以帮忙检查一下吗?

var arrName = ['aaa', 'bbb','ccc'];
var arrNumber = [3, 2, 4];
if (arrName.length != 0 && arrNumber.length != 0)
{            
    var table = document.createElement('table');
    var tbody = document.createElement('tbody');

    var tr_header = document.createElement('tr');

    var th_owner = document.createElement('th');
    var th_number = document.createElement('th');

    th_owner.appendChild(document.createTextNode('Owner'));
    th_number.appendChild(document.createTextNode('Number'));

    tr_header.appendChild(th_owner);
    tr_header.appendChild(th_number);

    tbody.appendChild(tr_header);

    for (var i = 0; i < arrName.length; i++)
    {
        var tr_details = document.createElement('tr');

        var td_owner = document.createElement('td');
        var td_count = document.createElement('td');

        td_owner.appendChild(document.createTextNode(arrName[i].toString()));
        td_count.appendChild(document.createTextNode(arrNumber[i].toString()));
        tr_details.appendChild(td_owner);
        tr_details.appendChild(td_count);

        tbody.appendChild(tr_details);
    }
    table.appendChild(tbody);
}
}

1 个答案:

答案 0 :(得分:0)

您的代码运行正常。也许您只是错过了将该表添加到页面中。 我只是在下面添加了以下代码:

var body = document.getElementsByTagName('body');
body[0].appendChild(table);

检查我编写的测试页:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
</head>
<body>

    <script>
        document.addEventListener('DOMContentLoaded', () => {
            var arrName = ['aaa', 'bbb','ccc'];
            var arrNumber = [3, 2, 4];
            if (arrName.length != 0 && arrNumber.length != 0)
            {

                var table = document.createElement('table');
                var tbody = document.createElement('tbody');

                var tr_header = document.createElement('tr');

                var th_owner = document.createElement('th');
                var th_number = document.createElement('th');

                th_owner.appendChild(document.createTextNode('Owner'));
                th_number.appendChild(document.createTextNode('Number'));

                tr_header.appendChild(th_owner);
                tr_header.appendChild(th_number);

                tbody.appendChild(tr_header);

                for (var i = 0; i < arrName.length; i++)
                {
                    var tr_details = document.createElement('tr');

                    var td_owner = document.createElement('td');
                    var td_count = document.createElement('td');

                    td_owner.appendChild(document.createTextNode(arrName[i].toString()));
                    td_count.appendChild(document.createTextNode(arrNumber[i].toString()));

                    tr_details.appendChild(td_owner);
                    tr_details.appendChild(td_count);

                    tbody.appendChild(tr_details);
                }

                table.appendChild(tbody);

                var body = document.getElementsByTagName('body');
                body[0].appendChild(table);
            }
        });
    </script>

</body>
</html>