博客列表 >简易留言板的实现

简易留言板的实现

安超
安超原创
2022年11月10日 11:32:48764浏览

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

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

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