Blogger Information
Blog 19
fans 0
comment 0
visits 10716
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
二个获取元素的api 与 dom常用的遍历方法
牙森江
Original
338 people have browsed it

1.两个获取元素的api

1.1 第一个获取元素的api-querySelectorAll

  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>获取dom元素</title>
  8. </head>
  9. <body>
  10. <ul class="item">
  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. <li>item7</li>
  18. <li>item8</li>
  19. <li>item9</li>
  20. </ul>
  21. <script>
  22. const items = document.querySelectorAll('.item > *')
  23. console.log(items, Array.isArray(items))
  24. items.forEach(item => (item.style.color = 'green'))
  25. </script>
  26. </body>
  27. </html>

运行效果:

1.2 第一个获取元素的api-querySelector

  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>获取dom元素</title>
  8. </head>
  9. <body>
  10. <ul class="item">
  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. <li>item7</li>
  18. <li>item8</li>
  19. <li>item9</li>
  20. </ul>
  21. <script>
  22. const item = document.querySelector('.item> *')
  23. console.log(item)
  24. </script>
  25. </body>
  26. </html>

运行效果:

2.dom常用的遍历方法

常用的遍历方法有三个:1.document 2.element 3.text 个类型

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