js中添加table

<html>
<head>
    <title></title>
<script type="text/javascript">
    function getInnerHTML() {
        // alert(document.getElementById("tr1").innerHTML);
        var input = document.createElement("input");
        input.type = "text";
        input.id = "txte1";
        document.body.insertBefore(input, t1);
        init();
    }
    function init() {
        var table = document.createElement("table");
        var tbody = document.createElement("tbody");
        for (var i = 1; i < 10; i++) {
            var row = document.createElement("tr");
            row.id = i;
            if (i!=4 && i%2) {
                row.bgColor = "#ffff00";
            } else if (i == 4) {
                row.bgColor = "#ff0000";
            } else {
                row.bgColor = "#0000ff";
            }
            for (var j = 1; j < 6; j++) {
                var cell = document.createElement("td");
                cell.id = i + "/" + j;
                cell.appendChild(document.createTextNode("第" + cell.id + "列"));
                row.appendChild(cell);
            }
            tbody.appendChild(row);
        }
        table.appendChild(tbody);
        document.body.insertBefore(table,t1);
    }
</script>
</head>
<body>

<table id="t1" border="1">
<tr id="tr1">
<th>Firstname<span style=" display:none;">sd</span></th>
<th>Lastname</th>
</tr>
<tr id="tr2">
<td>Peter</td>
<td>Griffin</td>
</tr>
</table>
<br />
<input type="button"
value="Alert innerHTML of table row" />

</body>
</html>


本文出自 “” 博客,请务必保留此出处http://8532016.blog.51cto.com/8522016/1551686

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。