美文网首页
动态创建表格(html)

动态创建表格(html)

作者: 琪33 | 来源:发表于2018-04-26 16:24 被阅读0次
 <style>
        table {
            border-collapse: collapse;
            border-spacing: 0;
            border: 1px solid #c0c0c0;
        }

        th,
        td {
            border: 1px solid #d0d0d0;
            color: #404060;
            padding: 10px;
        }

        th {
            background-color: #09c;
            font: bold 16px "微软雅黑";
            color: #fff;
        }

        td {
            font: 14px "微软雅黑";
        }

        tbody tr {
            background-color: #f0f0f0;
        }

        tbody tr:hover {
            cursor: pointer;
            background-color: #fafafa;
        }
    </style>
    <script src="jquery-1.11.1.js"></script>
    <script>
        $(function () {
            // 模拟从后台拿到的数据
            var data = [{
                name: "传智播客",
                url: "http://www.itcast.cn",
                type: "IT最强培训机构"
            }, {
                name: "黑马程序员",
                url: "http://www.itheima.com",
                type: "大学生IT培训机构"
            }, {
                name: "传智前端学院",
                url: "http://web.itcast.cn",
                type: "前端的黄埔军校"
            }];

            //需求:点击按钮,然后生成tr里面生成三个td.数组元素个数个tr。然后放入tbody中
            //步骤:
            //1.绑定事件
            //2.利用for循环,把数组中的所有数据组合成一个字符串。
            //3.把生成的字符串设置为html内容添加进tbody中。


            //1.绑定事件
            $("input").click(function () {
                //写入到click事件中,每次点击以后把str清空
                var str = "";
                //2.利用for循环,把数组中的所有数据组合成一个字符串。
                for(var i=0;i<data.length;i++){
                    //如何生成3组???
//                    str += "<tr><td>1</td><td>2</td><td>3</td></tr>";
                    //data[i] = 数组中的每一个json
                    //data[i].name = 数组中的每一个json的name属性值
                    str += "<tr><td>"+data[i].name+"</td><td>"+data[i].url+"</td><td>"+data[i].type+"</td></tr>";
                }

                //3.把生成的字符串设置为html内容添加进tbody中。
                $("#j_tbData").html(str);
            })
        })
    </script>
</head>

<body>
<input type="button" value="获取数据" id="j_btnGetData"/>
<table>
    <thead>
    <tr>
        <th>标题</th>
        <th>地址</th>
        <th>说明</th>
    </tr>
    </thead>
    <tbody id="j_tbData">
        <!--<tr>-->
            <!--<td>1</td>-->
            <!--<td>2</td>-->
            <!--<td>3</td>-->
        <!--</tr>-->
    </tbody>
</table>
</body>

相关文章

  • 2019-01-14

    //动态创建表格---JS API视图 title * { ma...

  • 动态创建表格(html)

  • HTML表格

    你可以使用HTML创建表格。 实例 表格 这个例子演示如何在 HTML 文档中创建表格。 表格边框 本例演示各种类...

  • 表格

    创建表格 在HTML网页中,要想创建表格,就需要使用表格相关的标签。创建表格的基本语法格式如下: 单元格内的...

  • 13

    动态创建表格 var json='[{"ename":"Tom", "salary":10000, "age":2...

  • HTML创建表格

    创建表格 想在网页上展示上述表格效果可以使用以下代码: 创建表格的四个元素: table、tbody、tr、th、...

  • 表格创建的方法

    创建表格,so easy啊,今天就创建表格这个小实例,来写写不一样的实现方法 html标签实现 js实现 js的方...

  • HTML之创建表格

    要求:用HTML实现如下所示的表格 对于初学者而言当看到这个表格的时候会不会有点懵呢?表头又是行合并,又是列合并的...

  • 用HTML创建表格

    用HTML建表格 ...(标题)... ... .... ... .... .... a...

  • jQuery动态创建表格

    效果:

网友评论

      本文标题:动态创建表格(html)

      本文链接:https://www.haomeiwen.com/subject/jgkmlftx.html