Blogger Information
Blog 19
fans 0
comment 0
visits 9832
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
留言板实战
newbie
Original
601 people have browsed it

效果图

代码

  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>留言板实战</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. /* border: 1px solid red; */
  14. }
  15. li {
  16. list-style: none;
  17. }
  18. .ground {
  19. display: grid;
  20. grid-template-columns: repeat(2, 1fr);
  21. place-items: center;
  22. }
  23. .ban {
  24. background-color: aqua;
  25. width: 490px;
  26. height: 500px;
  27. display: grid;
  28. grid-template-rows: repeat(2, 2fr);
  29. place-items: center;
  30. }
  31. .windows {
  32. background-color: aquamarine;
  33. width: 490px;
  34. height: 500px;
  35. }
  36. ul {
  37. position: relative;
  38. }
  39. input {
  40. padding: 60px;
  41. height: 100px;
  42. width: 400px;
  43. border-radius: 5px;
  44. outline: none;
  45. }
  46. button {
  47. width: 20px;
  48. height: 20px;
  49. font-size: 15px;
  50. line-height: 15px;
  51. text-align: center;
  52. border-radius: 50%;
  53. background-color: mediumvioletred;
  54. position: absolute;
  55. right: 0;
  56. }
  57. </style>
  58. </head>
  59. <body>
  60. <div class="ground">
  61. <div class="ban">
  62. <input
  63. type="text"
  64. placeholder="请输入留言"
  65. ds
  66. onkeydown="addMsg(this)"
  67. autofocus
  68. />
  69. <hr style="width: 100%" />
  70. <div class="zhanshi">
  71. <ul class="list" style="width: 485px; height: 365px"></ul>
  72. </div>
  73. </div>
  74. <div class="windows"></div>
  75. </div>
  76. <script>
  77. function addMsg(ele) {
  78. if (event.key === "Enter") {
  79. let ul = document.querySelector(".list"); //获取ul元素
  80. if (ele.value.trim().length === 0) {
  81. //判断内容是否为空。trim方法可以去除字符串中的空格
  82. alert("不能为空");
  83. ele.focus();
  84. return false;
  85. }
  86. const li = document.createElement("li");
  87. li.innerHTML =
  88. ele.value +
  89. `<button onclick = "del(this.parentNode)">x</button><hr>`; //添加一个删除按钮
  90. ul.insertAdjacentElement("afterBegin", li); //在上条留言上部插入一条留言
  91. ele.value = null; //清空输入框
  92. ele.focus(); //重置焦点
  93. }
  94. }
  95. function del(ele) {
  96. return confirm("是否删除本条留言?") ? ele.remove() : false;//判断是继续执行清除本元素的命令
  97. }
  98. </script>
  99. </body>
  100. </html>
Correcting teacher:PHPzPHPz

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