博主信息
博文 19
粉丝 0
评论 0
访问量 15458
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
留言板实战
newbie
原创
839人浏览过

效果图

代码

  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>
批改老师:PHPzPHPz

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学