javascript当中创建table的TD和创建TR的方法

例 2.3(CreateTRTDIEFF.html)

<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<body>
<table  border="1" >
    <tr>
        <td>id</td>
        <td>姓名</td>
    </tr>
      <tbody id="display">
        </tbody>
</table>

</body>
</html>
<script type="text/javascript">
    for (var i = 0; i < 3; i++) {
        var data = ['1', 'qixy'];
        var newRow = generaARow(data);
        var displayTable = document.getElementById("display");
        /*
         TBODY object (Object/HTML)
         An object that encapsulates a <TBODY> tag within a <TABLE> block. Inherits from: Element object,Element object (Object/HTML)
         Inherits from: Node object.
         Node.appendChild() (Method)
         A new child node object is added to the end of the list of immediate children of this node.
         JavaScript syntax: - myNode.appendChild(aNode)
         Argument list: aNode The node to be appended
         */
        displayTable.appendChild(newRow)
    }
    function generaARow(data) {
        var row, cell, txtNode;
        /*
         var a=document.createElement("div"); "div" also is a known element. you can not write as "divxxx". the same thing, we can use "table","select","option" ,etc.
         Document.createElement() (Method)
         A method to create a new element within a document.
         return value is:  Element object.  Element object is a subclass of Node. 
         */
        row = document.createElement("tr");
        for (var i = 0; i < data.length; i++) {
            cell = document.createElement("td");
            /*the following two statements's function is the same as the third statement. we can use either way to achieve goal.*/
            //    txtNode = document.createTextNode(data[i]);
            //    cell.appendChild(txtNode);
更多请见:http://www.mark-to-win.com/tutorial/js_7_createElementtr.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值