Blogger Information
Blog 9
fans 0
comment 0
visits 5382
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Js运用案例:ToList留言和动态生成表格案例-2019.01.18
大宝的博客
Original
594 people have browsed it

 ToList留言并添加删除功能:

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ToList添加删除功能</title>
</head>
<body>
    <h2>留言板</h2>
    <input type="text">
    <ul></ul>
    <script>
        var comment = document.querySelector('input');
        var ul = document.getElementsByTagName('ul')['0']; 
        comment.focus(); // 设置焦点
        comment.onkeydown = function(event){
            if(event.keyCode === 13){
                var li = document.createElement('li');
                //执行一段空白的javascript语句,返回空或者false值,从而防止链接跳转
                li.innerHTML = comment.value+'<a href="javascript:;" onclick="del(this)">' + '删除' + '</a>';
                if(ul.childElementCount === 0){
                    ul.appendChild(li);//向节点添加最后一个子节点
                }else{
                    // 如果列表已有留言,则插入到新一条之前
                    var first = ul.firstElementChild;
                    ul.insertBefore(li,first);
                }
                // 清空留言区,并设置焦点
                comment.value = '';
                comment.focus();
            }
        }
        function del(ele){
            if(confirm('确定要删除么?')){
               var li = ele.parentNode;
               li.parentNode.removeChild(li);
            }
            return false;
        }
    </script>
</body>
</html>

运行实例 »

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

用js自动生成表格:

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>js操作表格的基本技巧</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;
            }
            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>
                </tr>
            </thead>
            <tbody>
            </tbody>    
    </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 car = document.getElementById('cart');
            var tbody = car.children[2];  

        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>';
            tbody.appendChild(tr);
        })
        /*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:qualified

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