Correction status:Uncorrected
Teacher's comments:
<!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>ToList练习</title> </head> <body> <h3>留言板</h3> <input type="text"> <ul></ul> <script> var comment=document.querySelector('input'); var ul=document.getElementsByTagName('ul')[0]; comment.focus(); // event 代表事件对象 comment.onkeydown=function(event) { if (event.keyCode===13) { var li=document.createElement('li'); li.innerHTML=comment.value+'<a href="javascrip:;" onclick="del(this)">删除</a>'; if(ul.childElementCount===0){ ul.appendChild(li); } else { var first=ul.firstElementChild; ul.insertBefore(li,first); } comment.value =''; } } function del(ele){ if(confirm('要删除吗')) { ele.parentNode; var li=ele.parentNode; li.parentNode.removeChild(li); } } </script> </body> </html>
点击 "运行实例" 按钮查看在线实例