Blogger Information
Blog 16
fans 0
comment 0
visits 12598
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
5.9留言本
如花似玉的小牛牛的博客
Original
1153 people have browsed it

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>Title</title>
            <style>
                table,th,td{
                    border: 1px solid #666;
                }
                table {
                    width: 500px;
                    text-align: center;
                    border-collapse: collapse;
                }
                table caption {
                    font-size: 1.2rem;
                    margin-bottom: 15px;
                }
                /* 这里必须在nth-of-type(1)前添加父元素,否则thead,tbody中的第一行都会生效 */
                thead tr:nth-of-type(1) {
                    background-color: lightblue;
                }

            </style>
        </head>
<body>
<table id="cart">
    <caption>学习使我快乐</caption>
    <thead>
    <tr>
        <th>编号</th>
        <th>品名</th>
        <th>数量</th>
        <th>单价</th>
        <th>编辑</th>
    </tr>
    <tbody></tbody>
    </thead>
</table>
<script>

    var data = [
        {id: 1, name: '牛奶', count: 3, price: 50},
        {id: 1, name: '苹果', count: 10, price: 80},
        {id: 1, name: '衣服', count: 2, price: 600}
    ];
        var cart = document.getElementById('cart'); //获取cart
        var tobdy = cart.children[2]; 获取tobdy

    data.forEach(function (value) {
            var tr = document.createElement("tr");      //创建标签
          tr.innerHTML = '<td>' + value.id + '</td>';
          tr.innerHTML += '<td>' + value.name + '</td>';
           tr.innerHTML += '<td>' + value.count + '</td>';
          tr.innerHTML += '<td>' + value.price + '</td>';
            tobdy.appendChild(tr);              //插入tr
        });
</script>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post