博主信息
博文 18
粉丝 0
评论 0
访问量 11882
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
留言表单实战
只如初见
原创
617人浏览过

留言表单实战

html代码

  1. <div class="mess">
  2. <div class="messt">留言板</div>
  3. <div class="messc">
  4. <div><label for="title">请输入留言内容</label>
  5. <br>
  6. <input type="text" id="title" class="mess_text" onkeydown="addMsg(this)" placeholder="输入Enter或者点击提交按钮"
  7. value="" />
  8. </div>
  9. <div><button onclick="msg()">提交留言</button></div>
  10. </div>
  11. </div>
  12. <div class="lycon">
  13. <div class="messt">留言内容展示</div>
  14. <div class="messc">
  15. <ul class="lylist">
  16. </ul>
  17. </div>
  18. </div>

css代码

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. }
  6. a {
  7. text-decoration: none;
  8. color: #555;
  9. }
  10. li {
  11. list-style: none;
  12. }
  13. .mess,
  14. .lycon {
  15. width: 560px;
  16. margin: 100px auto 0 auto;
  17. background-color: #f8f8f8;
  18. border-radius: 5px;
  19. }
  20. .mess .messt,
  21. .lycon .messt {
  22. width: 100%;
  23. height: 65px;
  24. line-height: 65px;
  25. padding: 0 40px;
  26. background-color: #9fc560;
  27. color: #fff;
  28. font-size: 20px;
  29. border-top-left-radius: 5px;
  30. border-top-right-radius: 5px;
  31. }
  32. .messc {
  33. padding: 40px;
  34. }
  35. .messc input[type="text"] {
  36. width: 100%;
  37. height: 35px;
  38. border: 1px #e5e5e5 solid;
  39. margin: 10px 0 20px 0;
  40. text-indent: 10px;
  41. font-size: 14px;
  42. color: #333;
  43. }
  44. .messc button {
  45. width: 100px;
  46. height: 35px;
  47. text-align: center;
  48. line-height: 35px;
  49. border: 0;
  50. background-color: #9fc560;
  51. color: #fff;
  52. border-radius: 3px;
  53. cursor: pointer;
  54. }
  55. .messc button:hover {
  56. background-color: #739e2b;
  57. color: #fff;
  58. }
  59. .lycon {
  60. margin: 20px auto;
  61. }
  62. .lycon .messt {
  63. height: 40px;
  64. line-height: 40px;
  65. font-size: 16px;
  66. background-color: #7aa534;
  67. }
  68. .lylist button {
  69. margin-left: 12px;
  70. }
  71. .lylist li {
  72. margin-bottom: 10px;
  73. }

js代码

  1. <script>
  2. function addMsg(tit) {
  3. if (event.key === 'Enter') {
  4. const ul = document.querySelector('.lylist');
  5. if (tit.value.trim().length === 0) {
  6. alert("留言不能为空!");
  7. tit.focus();
  8. return false;
  9. }
  10. const li = document.createElement('li');
  11. li.innerHTML = tit.value + '<button onclick="del(this.parentNode)">删除</button';
  12. ul.insertAdjacentElement('afterBegin', li);
  13. tit.value = null;
  14. tit.focus();
  15. }
  16. }
  17. function msg() {
  18. const ul = document.querySelector('.lylist');
  19. const tit = document.querySelector('.mess_text');
  20. //console.log(tit);
  21. if (tit.value.trim().length === 0) {
  22. alert("留言不能为空!");
  23. tit.focus();
  24. return false;
  25. }
  26. const li = document.createElement('li');
  27. li.innerHTML = tit.value + '<button onclick="del(this.parentNode)">删除</button';
  28. ul.insertAdjacentElement('afterBegin', li);
  29. tit.value = null;
  30. tit.focus();
  31. }
  32. function del(tit) {
  33. return confirm('是否删除?') ? tit.remove() : false;
  34. }
  35. </script>

效果图

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