Blogger Information
Blog 18
fans 0
comment 2
visits 8755
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
简易留言板
弦知音的博客
Original
827 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. </head>
  9. <style>
  10. :root {
  11. margin: 0;
  12. padding: 0;
  13. box-sizing: border-box;
  14. }
  15. .box {
  16. width: 450px;
  17. position: fixed;
  18. }
  19. .box fieldset {
  20. background-color: lightseagreen;
  21. height: 300px;
  22. padding: 20px 30px;
  23. outline: none;
  24. border: 1px solid #ddd;
  25. }
  26. .box fieldset label {
  27. font-size: 18px;
  28. color: #fff;
  29. }
  30. .box fieldset legend {
  31. font-size: 16px;
  32. background-color: lightsalmon;
  33. color: white;
  34. padding: 8px 20px;
  35. }
  36. .box fieldset textarea {
  37. width: 450px;
  38. outline: none;
  39. border: 1px solid #ddd;
  40. margin: 10px auto;
  41. }
  42. .box fieldset button {
  43. border: none;
  44. background-color: lightsalmon;
  45. color: #fff;
  46. font-size: 16px;
  47. width: 100px;
  48. height: 30px;
  49. line-height: 30px;
  50. position: absolute;
  51. right: 20px;
  52. bottom: 20px;
  53. cursor: pointer;
  54. }
  55. .list {
  56. width: 500px;
  57. height: 326px;
  58. background-color: lightgreen;
  59. position: fixed;
  60. left: 540px;
  61. }
  62. </style>
  63. <script>
  64. function addMsg() {
  65. // 1.获取留言内容
  66. let msg = document.getElementById("yourMsgs");
  67. // 2.获取显示留言的容器
  68. const ul = document.querySelector('.list');
  69. // 3.判断用户留言是否为空
  70. if (msg.value.trim().length === 0){
  71. alert("留言不能为空");
  72. msg.focus;
  73. //如果不用 return false 则会继续继续执行后面的代码,增加一个空节点
  74. return false;
  75. }
  76. // 4.添加一条新留言
  77. const li = document.createElement('li');
  78. li.innerHTML = msg.value + '<button onclick="del(this.parentNode)">删除</button>';
  79. ul.insertAdjacentElement('afterBegin',li);
  80. // 5.将输入框中的前一条留言清空
  81. msg.value = null;
  82. // 6.重置输入框焦点
  83. msg.focus;
  84. }
  85. function del(ele){
  86. // ele.remove(); //也可以用 ele.outerHTML = null;
  87. return confirm("您确定删除该留言吗?") ? ele.remove(): false;
  88. }
  89. function myMsg(ele) {
  90. if (event.key === 'Enter'){
  91. // 1.获取显示留言的容器
  92. const ul = document.querySelector('.list');
  93. // 3.判断用户留言是否为空
  94. if (ele.value.trim().length === 0){
  95. alert("留言不能为空");
  96. ele.focus;
  97. //如果不用 return false 则会继续继续执行后面的代码,增加一个空节点
  98. return false;
  99. }
  100. // 4.添加一条新留言
  101. const li = document.createElement('li');
  102. li.innerHTML = ele.value + '<button onclick="del(this.parentNode)">删除</button>';
  103. ul.insertAdjacentElement('afterBegin',li);
  104. // 5.将输入框中的前一条留言清空
  105. ele.value = null;
  106. // 6.重置输入框焦点
  107. ele.focus;
  108. }
  109. }
  110. </script>
  111. <body>
  112. <div class="box">
  113. <fieldset>
  114. <legend>留言板</legend>
  115. <label for="yourMsgs">请输入您的留言</label>
  116. <textarea name="msgs" id="yourMsgs" onkeydown="myMsg(this)" cols="30" rows="10" placeholder="请输入您的留言" autofocus></textarea>
  117. <button onclick="addMsg()">提交留言</button>
  118. </fieldset>
  119. </div>
  120. <ul class="list"></ul>
  121. </body>
  122. </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