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

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<label for="liuyan">请留言:</label>
<input type="text" id="liuyan" onkeydown="addliuyan(this)" autofocus  >
<ul id="list"></ul>
    <script>
        function addliuyan(liuyan) {
            console.log(event.keyCode);
            if(event.keyCode === 13) {            //回车

                var  item = document.createElement("li");            //创建li标签

                item.innerText = liuyan.value;      //item 内容获取
                item.innerHTML += '<button onclick="del(this)">删除</button> ';
                var list = document.querySelector("#list");//获取ul标签

                if (list.childElementCount === 0){       //子节点数量
                list.appendChild(item);     //添加列表项
                }else {
                    var first = list.firstElementChild; // 获取第一个节点
                    list.insertBefore(item, first); //itme 插入第一个节点之前
                };
            }
        }

        function del(ele) {
            if (confirm('是否删除:')){
                var li =ele.parentNode;         //获取按钮复建点
                li.parentNode.removeChild(li);  //获取li节点的父节点删除li
            }
        }
    </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