博客列表 >留言表单实战

留言表单实战

只如初见
只如初见原创
2022年04月05日 11:21:44435浏览

留言表单实战

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>

效果图

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议