Blogger Information
Blog 13
fans 0
comment 0
visits 6513
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js留言板
shooter
Original
636 people have browsed it

留言板

效果图

代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  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. }
  14. body .lyb .srk input {
  15. padding: 30px;
  16. height: 50px;
  17. width: 200px;
  18. border-radius: 5px;
  19. outline: none;
  20. }
  21. .lyb {
  22. width: 50%;
  23. height: 100%;
  24. background-color: cadetblue;
  25. }
  26. .srk {
  27. display: grid;
  28. place-content: center;
  29. height: 100px;
  30. }
  31. ul {
  32. position: relative;
  33. }
  34. li {
  35. list-style: none;
  36. }
  37. button {
  38. font-size: 15px;
  39. line-height: 15px;
  40. text-align: center;
  41. border-radius: 50%;
  42. background-color: mediumvioletred;
  43. position: absolute;
  44. right: 0;
  45. }
  46. </style>
  47. </head>
  48. <body>
  49. <div class="lyb">
  50. <div class="srk">
  51. <input
  52. type="text"
  53. onkeydown="addMsg(this)"
  54. placeholder="请输入类容"
  55. autofocus
  56. />
  57. </div>
  58. <ul class="list"></ul>
  59. </div>
  60. <script>
  61. function addMsg(ele) {
  62. if (event.key === 'Enter') {
  63. // 获取留言板类容
  64. const ul = document.querySelector('.list');
  65. //判断用户留言是否为空
  66. if (ele.value.trim().length === 0) {
  67. alert('留言不能为空');
  68. ele.focus();
  69. return false;
  70. }
  71. //添加一条留言
  72. const li = document.createElement('li');
  73. li.innerHTML =
  74. ele.value + '<button onclick="del(this.parentNode)">删除</button>';
  75. ul.insertAdjacentElement('afterBegin', li);
  76. //将输入框中的前一条留言清空
  77. ele.value = null;
  78. //焦点重置
  79. ele.focus();
  80. }
  81. }
  82. //删除
  83. function del(ele) {
  84. return confirm('是否删除' ? ele.remove() : false);
  85. }
  86. </script>
  87. </body>
  88. </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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!