Blogger Information
Blog 25
fans 0
comment 0
visits 13677
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
简易留言板的实现
安超
Original
772 people have browsed it

本文实现一个简易的留言板

本文的看点在于实现了留言的动态插入。
效果图如下:
留言板效果

  1. <style>
  2. /* 留言框 */
  3. form fieldset{
  4. width: 200px;
  5. }
  6. /* 留言区CSS */
  7. .container{
  8. /* border: 1px solid gray; */
  9. max-width: 600px;
  10. }
  11. .container .note{
  12. border: 1px solid burlywood;
  13. display: grid;
  14. }
  15. .container .note div:last-of-type{
  16. text-align: right;
  17. font-size: x-small;
  18. font-family: 'Courier New', Courier, monospace;
  19. }
  20. .container .reply{
  21. border-bottom: 2px solid red;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <!-- 1. 改写留言板案例,适当添加CSS美化 2. (选做), 将留言板案例改成"智能客服系统",实现机器人回复 -->
  27. <h4>意见框:</h4>
  28. <form action="#" method="post" name="myForms_1" >
  29. <fieldset>
  30. <legend>请留下您的宝贵意见:</legend>
  31. <textarea name="note" id="note" cols="30" rows="10" placeholder="留言后点击'提交'" autofocus></textarea><br><br>
  32. <button type="button" onclick = "tijiao()">提交</button>
  33. </fieldset>
  34. </form>
  35. <br>
  36. <br>
  37. <!-- 以下为留言区 -->
  38. <p>留言区</p>
  39. <div class="container">
  40. <div class="note">
  41. <div></div>
  42. <div></div>
  43. </div>
  44. <div class="reply"></div>
  45. </div>
  1. // 获取form表单
  2. let forms = document.forms.myForms_1;
  3. // 获取留言区的DOM元素
  4. let container = document.querySelector('.container');
  5. let note = document.querySelector('.note');
  6. let note_1 = note.firstElementChild;
  7. let note_2 = note.lastElementChild;
  8. let reply = document.querySelector('.reply');
  9. // 留言提交函数
  10. function tijiao(){
  11. let trLength = note.children.length;
  12. console.log(trLength);
  13. console.log(forms.note.value);
  14. let value = forms.note.value;
  15. let date = new Date();
  16. let time = date.getFullYear()+"年"+date.getMonth()+"月"+date.getDay()+"日"+date.getHours()+"点"+date.getMinutes()+"分";
  17. // 将内容放到模板字面量里
  18. let noteDiv = `<div class = "note"><div>${value}</div><div>${time}</div></div><div class="reply">"您好,你的留言已经收到"</div>`;
  19. // 将内容插入留言区
  20. container.insertAdjacentHTML("afterbegin",noteDiv);
  21. forms.note.value="";
  22. }
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