博客列表 >改写留言板案例,适当添加CSS美化

改写留言板案例,适当添加CSS美化

P粉751989631
P粉751989631原创
2022年11月14日 22:47:45438浏览

改写留言板案例,适当添加CSS美化

  1. <!DOCTYPE html>
  2. <html lang="zh-cn">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>todolist:留言板</title>
  8. </head>
  9. <body>
  10. <input type="text" onkeydown="insertComment(this)" placeholder="请输入留言" autofocus />
  11. <ul class="list"></ul>
  12. <script>
  13. const insertComment = function (ele) {
  14. // console.log(enent);
  15. // console.log(enent.type);
  16. //
  17. if (event.key === "Enter") {
  18. if (ele.value.length === 0) {
  19. alert("留言不能为空");
  20. ele.focus();
  21. return false;
  22. }
  23. const ul = document.querySelector(".list");
  24. ele.value += `<button onclick="del(this.parentNode)">删除</button>`;
  25. ul.insertAdjacentHTML("afterbegin", `<li>${ele.value}</li>`);
  26. ele.value = null;
  27. }
  28. };
  29. const del = function (ele) {
  30. return confirm("是否删除?") ? (ele.outerHTML = null) : false;
  31. };
  32. </script>
  33. </body>
  34. </html>

运行结果

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议