另一种动态创建表格的方法(insertRow(-1)-insertCell())

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_32077521/article/details/97615689
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <meta charset="utf-8" />
</head>
<body>
    <button id="btn">动态创建表格</button>
    <script type="text/javascript">
        window.onload = function () {
            document.getElementById('btn').onclick = function () {
                var jsontxt = { "百度": "http://www.baidu.com", "饿了么": "http://www.ele.me", "谷歌": "http://www.google.com" };
                var table = document.createElement('table');
                table.border = '1px';
                for (var i in jsontxt) {
                    var currentRow = table.insertRow(-1);
                    currentRow.insertCell(-1).innerHTML = i;
                    currentRow.insertCell(-1).innerHTML = '<a href="' + jsontxt[i] + '" target="_blank">' + i + '</a>';
                }
                document.body.appendChild(table);
            };
        };
    </script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_32077521/article/details/97615689