博主信息
博文 46
粉丝 0
评论 0
访问量 47544
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
Dom实战:留言板
上草一方
原创
494人浏览过

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>

效果如下图:

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