博客列表 >dom增删改,留言板应用

dom增删改,留言板应用

喜
原创
2022年07月29日 15:52:27500浏览

1.dom增删改


insertAdjacentElement 添加元素

insertAdjacentHTML与cloneNode 克隆节点

替换节点与删除节点

2.应用留言板

  1. <!DOCTYPE html>
  2. <html lang="en">
  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>Document</title>
  8. </head>
  9. <style>
  10. *{
  11. margin: 0;
  12. padding: 0;
  13. box-sizing: border-box;
  14. }
  15. .ly{
  16. width: 900px;
  17. margin: 0 auto;
  18. }
  19. textarea{
  20. width: 100%;
  21. height: 112px;
  22. resize: none;
  23. border-radius: 10px;
  24. padding-top: 10px;
  25. padding-left: 20px;
  26. outline: none;
  27. font-size: 18px;
  28. }
  29. .bott{
  30. display: flex;
  31. justify-content: flex-end;
  32. align-items: center;
  33. }
  34. button{
  35. width: 100px;
  36. height: 30px;
  37. background-color: #0084ff;
  38. border: none;
  39. color: #fff;
  40. font-family: 宋体;
  41. font-size: 14px;
  42. font-weight: bold;
  43. transition: all 0.5s;
  44. }
  45. button:hover {
  46. background: rgb(0, 225, 255);
  47. }
  48. p{
  49. margin-right: 5px;
  50. color: #666;
  51. }
  52. span{
  53. color: #ff0000;
  54. padding-right: 5px;
  55. }
  56. .info{
  57. margin: 40px auto 0;
  58. }
  59. .xx{
  60. width: 900px;
  61. margin: 0 auto;
  62. padding: 20px 0;
  63. border-bottom: 1px dashed #ccc;
  64. }
  65. .xx .top{
  66. display: flex;
  67. justify-content: space-between;
  68. }
  69. .xx .left{
  70. display: flex;
  71. height: 84px;
  72. }
  73. .xx .left img{
  74. height: 100%;
  75. border-radius: 50%;
  76. }
  77. .xx .right{
  78. margin-left: 20px;
  79. align-self: center;
  80. }
  81. .xx .right h3{
  82. font: bold 16px '宋体';
  83. }
  84. .xx .right p{
  85. color: #aaa;
  86. margin-top: 8px;
  87. font-size: 12px;
  88. }
  89. .close span{
  90. font-size: 28px;
  91. }
  92. .bottom{
  93. padding-left: 100px;
  94. color: #666;
  95. word-break: break-all;
  96. }
  97. </style>
  98. <body>
  99. <div class="ly">
  100. <div class="img">
  101. <img src="./images/tip.png" alt="">
  102. </div>
  103. <textarea name="" id="" cols="30" rows="10" placeholder="说点什么吧..." maxlength="200"></textarea>
  104. <div class="bott">
  105. <p><span>0</span>/ 200</p>
  106. <button>发布</button>
  107. </div>
  108. </div>
  109. <div class="box1">
  110. <div class="info">
  111. </div>
  112. </div>
  113. </div>
  114. <script>
  115. let textarea=document.querySelector('textarea')
  116. let span=document.querySelector('.bott span')
  117. textarea.addEventListener('input',()=>span.innerHTML= textarea.value.trim().length)
  118. let button=document.querySelector('.bott button')
  119. button.addEventListener('click',()=>{
  120. if(textarea.value.trim()===''){
  121. textarea.value=''
  122. span.innerText=0
  123. alert('请输入内容')
  124. textarea. focus()
  125. return false
  126. }
  127. textarea.focus()
  128. let xx=document.createElement('div')
  129. xx.classList='xx'
  130. xx.innerHTML=`
  131. <div class="top">
  132. <div class="left">
  133. <div class="img"> <img src="https://img.php.cn/upload/avatar/000/991/210/62d10769ad48d159.jpg" alt=""></div>
  134. <div class="right">
  135. <h3>喜</h3>
  136. <p>${new Date().toLocaleString()}</p>
  137. </div>
  138. </div>
  139. <div class="close">
  140. <span>
  141. X
  142. </span>
  143. </div>
  144. </div>
  145. <div class="bottom">
  146. <p>${textarea.value}</p>
  147. </div>
  148. `
  149. document.querySelector('.info').insertAdjacentElement('afterBegin',xx)
  150. textarea.value=null
  151. span.innerHTML=0
  152. let del=document.querySelector('.close span')
  153. del.addEventListener('click',()=>document.querySelector('.info').removeChild(xx))
  154. })
  155. textarea.addEventListener('keyup',()=>{
  156. if(event.key==='Enter')button.click()
  157. })
  158. </script>
  159. </body>
  160. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议