Blogger Information
Blog 34
fans 0
comment 0
visits 20292
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js之留言板实例
小庄
Original
1043 people have browsed it

js之留言板实例

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>留言板</title>
  8. <link rel="stylesheet" href="style2.css">
  9. </head>
  10. <body>
  11. <form class="comment" id="comment">
  12. <label for="content">请留言:</label>
  13. <textarea class="content" name="content" id="content" cols="30" rows="5" placeholder="不要超过100个字符"></textarea>
  14. <button class="submit" type="button" name="submit">提交</button>
  15. </form>
  16. <!-- <button class="reg">事件注册</button> -->
  17. <ul class="list">
  18. </ul>
  19. <script>
  20. // reg.forEach(function (an){
  21. // console.log(an.classList);
  22. // });
  23. // reg.forEach(an => {
  24. // an.addEventListener('click',() => {
  25. // console.log(an.className)
  26. // console.log(an.classList)
  27. // })
  28. // });
  29. //留言板
  30. const btn = document.querySelector('button');
  31. const ul = document.querySelector('ul');
  32. const tx = document.querySelector('.content');
  33. btn.addEventListener('click',()=>{
  34. if(tx.value.length > 0 && tx.value.length <= 100){
  35. //提交留言
  36. let li = document.createElement('li');
  37. li.style.borderBottom = '1px solid white';
  38. li.style.minHeight = '3em'
  39. li.textContent = tx.value;
  40. //删除留言
  41. const delbtn = document.createElement('button');
  42. delbtn.textContent = "删除留言";
  43. delbtn.style.float = 'right'
  44. delbtn.classList.add('del-btn');
  45. delbtn.onclick = function (){
  46. if(confirm('是否删除该留言?')){
  47. this.parentNode.remove();
  48. alert('删除成功!')
  49. return false;
  50. }
  51. };
  52. ul.prepend(li);
  53. li.append(delbtn);
  54. tx.value = null;
  55. }else {
  56. alert("留言字符至少1个字符,最大100个字符")
  57. }
  58. });
  59. </script>
  60. </body>
  61. </html>
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!