Blogger Information
Blog 87
fans 1
comment 0
visits 58859
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
留言板学习
阿杰
Original
522 people have browsed it

留言板

  • css部分
  1. *{
  2. margin: 0;
  3. padding: 0;
  4. list-style: none;
  5. }
  6. .messCont{
  7. width: 300px;
  8. min-height: 300px;
  9. background-color: bisque;
  10. margin: 10vh auto;
  11. border-radius: 10px;
  12. padding: 10px;
  13. }
  14. .title,.inputCont{
  15. text-align: center;
  16. line-height: 3vh;
  17. }
  18. .list{
  19. margin-top: 2vh;
  20. }
  21. .set{
  22. margin: 10px 0 10px 20px;
  23. }
  • html部分
  1. <div class="messCont">
  2. <div class="title">留言板</div>
  3. <div class="inputCont">
  4. <input type="text" onkeydown="addMsg(this)" placeholder="请输入内容" autofocus />
  5. </div>
  6. <ul class="list"></ul>
  7. </div>
  • js部分
  1. function addMsg(ele) {
  2. // console.log(event);
  3. // console.log(event.key);
  4. if (event.key === 'Enter') {
  5. // 1. 获取显示留言的容器
  6. const ul = document.querySelector('.list');
  7. // 2. 判断用户留言是否为空
  8. if (ele.value.trim().length === 0) {
  9. alert('留言不能为空');
  10. ele.focus();
  11. return false;
  12. }
  13. // 3. 添加一条新留言
  14. const li = document.createElement('li');
  15. // console.log(ele.value);
  16. // li.textContent = ele.value;
  17. li.innerHTML = '<div>' + '留言:'+ ele.value + '<button onclick="del(this.parentNode)">删除</button>'+'</div>';
  18. li.className = 'item';
  19. // if (ul.firstElementChild !== null) {
  20. // ul.firstElementChild.before(li);
  21. // } else {
  22. // ul.append(li);
  23. // }
  24. ul.insertAdjacentElement('afterBegin', li);
  25. // 4. 将输入框中的前一条留言清空
  26. ele.value = null;
  27. // 5. 焦点重置
  28. ele.focus();
  29. // 回复
  30. setTimeout(function(){
  31. const replay = document.createElement('div');
  32. replay.className = 'set';
  33. replay.textContent = '感谢留言!';
  34. let lastEle = document.querySelector('.list>.item:first-of-type>div');
  35. lastEle.insertAdjacentElement('afterEnd', replay);
  36. },1500);
  37. }
  38. }
  39. // 删除
  40. function del(ele) {
  41. return confirm('是否删除?') ? ele.remove() : false;
  42. }
  • 效果

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