Blogger Information
Blog 46
fans 0
comment 0
visits 34461
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Dom实战:留言板
上草一方
Original
337 people have browsed it

dom实战小应用:留言板

演示代码如下:

  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>dom实战:留言板</title>
  8. <style>
  9. .user{
  10. width: 500px;
  11. height: 300px;
  12. border: 1px solid lightblue;
  13. position: relative;
  14. top: 10px;
  15. padding: 50px;
  16. margin: auto;
  17. }
  18. .textBox{
  19. width: 500px;
  20. height: 250px;
  21. border: 1px solid #CBA4C3;
  22. margin: 0 auto;
  23. }
  24. h3{
  25. text-align: center;
  26. position: relative;
  27. top: 10px;
  28. }
  29. p{
  30. position: absolute;
  31. left: 465px;
  32. margin-top: 20px;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <!-- <input type="text" onkeydown="console.log(event.key)" placeholder="请输入内容" autofocus>
  38. <ul class="list"></ul> -->
  39. <h3>留言板</h3>
  40. <div class="user">
  41. <textarea class="textBox" onkeydown="addMsg(this)" placeholder="请输入内容" cols="30" rows="10" autofocus></textarea>
  42. <ul class="list"></ul>
  43. </div>
  44. <p>感谢您的反馈!</p>
  45. <script>
  46. function addMsg(ele){
  47. // console.log(event);
  48. // console.log(event.key);
  49. if (event.key==='Enter'){
  50. // 1. 获取显示留言的容
  51. const ul=document.querySelector('.list');
  52. //2. 判断用户留言是否为空
  53. if (ele.value.trim().length===0){
  54. alert('用户留言不能为空');
  55. ele.focus();
  56. return false;
  57. }
  58. //3. 添加一条新留言
  59. const li=document.createElement('li');
  60. //console.log(ele.value);
  61. // li.textContent=ele.value;
  62. li.innerHTML=ele.value+'<button onclick="del(this.parentNode)">删除</button>';
  63. ul.insertAdjacentElement('afterBegin',li);
  64. //4. 将输入框中的前一条留言清空
  65. ele.value=null;
  66. //5. 焦点重置
  67. ele.focus();
  68. }
  69. }
  70. //删除
  71. function del(ele){
  72. // console.log(ele);
  73. // ele.remove();
  74. return confirm('是否删除?') ? ele.remove() : false;
  75. }
  76. </script>
  77. </body>
  78. </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