Blogger Information
Blog 18
fans 0
comment 0
visits 7928
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
留言表单实战
只如初见
Original
446 people have browsed it

留言表单实战

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>

效果图

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post