效果图

代码

  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>

更多相关文章

  1. Android启动界面实现
  2. 留言板功能
  3. 留言板与自动客服
  4. javascript-基础(四)实战
  5. Dom实战:留言板
  6. 留言板及自动客服回复功能的实例演示
  7. 留言板实践代码
  8. JS留言板实践
  9. 右侧布局案例

随机推荐

  1. Android(安卓)关于百度地图Android(安卓)
  2. 可以看到android源码的网站
  3. 安卓布局详细
  4. https://developers.google.com/chrome/m
  5. android中使用wakelock
  6. 一个很有深度的Android Blog
  7. Android 新手入门指导
  8. android 签名
  9. Android studio生成APK打包,修改生成APK的
  10. Android 支持的文件类型