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

留言板

效果图

代码

  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>
批改老师: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+教程免费学