Blogger Information
Blog 35
fans 0
comment 0
visits 25528
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
留言增加与删除,动态生成表格----2019年5月9日22时05分
白守的博客
Original
483 people have browsed it

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <label for="comment">留言内容</label>
    <input type="text" id="comment" onkeydown="addComment(this)" autofocus>

    <ul id="list"></ul>


    <script>
// 增加留言
        function addComment (comment){
            // console.log(event.keyCode);
        if (event.keyCode === 13) {

            // 创建一个li
            var item = document.createElement('li');


            item.innerHTML = comment.value;
            item.innerHTML += '<button onclick="del1(this)">删除</button>';
            // 获取第一个ul ,ul绑定的cssID是 list
            var list = document.querySelector('#list');

            if(list.childElementCount === 0){
                // 如果这是第一条则不做处理直接插入
                list.append(item);
            }else{
                var first = list.firstElementChild;
                list.prepend(item,first);
            }
            
             //  清空文本框
            comment.value = '';
            }
        }
    
    </script>
    

    <script>
    function del1(wh){
        return confirm('是否删除?') ? wh.parentElement.remove() : false;
    }

    </script>



<table id="cart1">

    <table id="cart">
        <caption>水果清单</caption>
        <thead>
            <tr>
                <th>编号</th>
                <th>品名</th>
                <th>数量</th>
                <th>单价</th>
            </tr>
        </thead>   
        <tbody></tbody>
    </table>



    <script>
    var data = [
        {id:1,name:'西瓜',count:3,price:9},
        {id:2,name:'苹果',count:5,price:8},
        {id:3,name:'香蕉',count:7,price:15},
        {id:4,name:'葡萄',count:8,price:27},
        {id:5,name:'橘子',count:4,price:85},
        {id:6,name:'芒果',count:2,price:65},
        {id:7,name:'木瓜',count:4,price:521},
        {id:8,name:'火龙果',count:8,price:45},
        {id:9,name:'西红柿',count:1,price:56},
        {id:10,name:'大溪谷',count:10,price:5},
        {id:11,name:'5',count:100,price:88},
        {id:12,name:'7',count:105,price:9}
    
    ];

    var cart = document.getElementById('cart');
    var tbody = cart.tBodies[0];
    for(var i =0; i < data.length; i++){
        var tr = document.createElement('tr');
         // 表格数据的第一行是一个对象,对象是根据属性名来访问
            // 只要获取到属性名组成的数组,遍历一下这个对象就可以生成一行数据啦
        Object.keys(data[i]).forEach(function(key){
            tr.innerHTML += '<td>' + data[i][key] + '</td>';
        })
        tbody.appendChild(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