Blogger Information
Blog 24
fans 0
comment 0
visits 10966
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
获取元素和遍历dom的方法
皮皮志
Original
348 people have browsed it
  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>Document</title>
  8. </head>
  9. <body>
  10. <ul class="list">
  11. <li>item1</li>
  12. <li>item2</li>
  13. <li>item3</li>
  14. <li>item4</li>
  15. <li>item5</li>
  16. <li>item6</li>
  17. </ul>
  18. <script>
  19. //获取元素
  20. const items = document.querySelectorAll('.list>*')
  21. items.forEach(ele=>ele.style.color='blue')
  22. const item = document.querySelector('.list > *')
  23. item.style.border ='1px solid red'
  24. </script>
  25. <script>
  26. let list = document.querySelector('.list')
  27. //获取所有子节点
  28. let node = list.childNodes
  29. //获取所有子元素节点
  30. let childNode = list.children
  31. //获取第一个元素
  32. let firstNode = list.firstElementChild
  33. firstNode.style.color = 'yellow'
  34. //获取后一个元素
  35. let nextNode = list.querySelector(':first-child + *')
  36. nextNode.style.color='green'
  37. //获取最后一个元素
  38. let lastNode = list.lastElementChild
  39. lastNode.style.color = 'lightgreen'
  40. //获取前一个元素
  41. let prevNode = lastNode.previousElementSibling
  42. prevNode.style.color = 'black'
  43. //获取父节点
  44. let parentNode = lastNode.parentNode
  45. parentNode.style.border = '1px solid blue'
  46. </script>
  47. </body>
  48. </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