博主信息
博文 35
粉丝 0
评论 0
访问量 31168
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
留言增加与删除,动态生成表格----2019年5月9日22时05分
白守的博客
原创
615人浏览过

实例

<!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>

运行实例 »

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


批改状态:未批改

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学