javascript 动态创建表格

编程学习 2021-07-04 21:47www.dzhlxh.cn编程入门
这篇文章主要介绍了javascript 动态创建表格,需要的朋友可以参考下

代码如下:

<html>
        <head>
            <script>
                function createTable(rows,lines){
                    this.rows=rows;
                    this.lines=lines;
                    var Body=document.getElementById('body');
                    var Table=document.createElement('table');//创建table标签元素
                    Table.setAttribute('border','1');
                    //给table标签添加其他属性
                    for(var i=0;i<this.rows;i++){
                        var lRow=document.createElement('tr');
                        for(var j=0;j<this.lines;j++){
                            var textNode=document.createTextNode(i+','+j);
                            var lLine=document.createElement('td');
                                lLine.appendChild(textNode);
                            lRow.appendChild(lLine);
                        }
                        Table.appendChild(lRow);
                    }
                    Body.appendChild(Table);
                }
            </script>
        </head>
        <body >
            <div id="body"></div>
        </body>
        <script type="text/javascript">
            createTable(10,10);
        </script>
</html>

第二种方法:

代码如下:

<script>
                function createTable(rows,lines){
                    this.rows=rows;
                    this.lines=lines;
                    var Body=document.getElementById('body');
                    var Table=document.createElement('table');
                    Table.setAttribute('border',1);
                    for(var i=0;i<this.rows;i++){
                        var row=Table.insertRow(i);
                        for(var j=0;j<this.lines;j++){
                            var cells=row.insertCell(j);
                            cells.innerHTML=i+','+j
                        }
                    }
                    Body.appendChild(Table);
                   
                }
            </script>

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板