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

1.两个获取元素的api

  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="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. <script>
  18. // 1.querySelectorAll:集合
  19. const items = document.querySelectorAll(".list>li");
  20. // items : 类数组(对象)
  21. console.log(items, Array.isArray(items));
  22. items.forEach(item => (item.style.border = "1px solid blue"));
  23. // 2.querySelector:集合中的第一个
  24. // 匹配满足条件的集合的第一个元素
  25. const item = document.querySelector(".list>li");
  26. console.log(item);
  27. item.style.color = "red";
  28. const list = document.querySelector(".list");
  29. console.log(list);
  30. const lis = list.querySelectorAll("li");
  31. console.log(lis);
  32. lis.forEach(li => (li.style.color = "green"));
  33. </script>
  34. </ul>
  35. </body>
  36. </html>

2.dom常用的遍历方法

  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="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. * *节点类型(12个,常用3个)
  21. * * 1. document: 文档类型, 9
  22. * * 2. element: 元素类型, 1
  23. * * 3. text: 文本类型, 3
  24. **/
  25. let list = document.querySelector(".list");
  26. console.log(list.nodeType);
  27. // 1.所有子节点:childNodes
  28. let items = list.childNodes;
  29. console.log(items);
  30. // 2.所有子元素节点:children
  31. console.log(list.children);
  32. // 3.第一个子元素;firstElementChild
  33. console.log(list.children[0].textContent);
  34. console.log(list.firstElementChild.textContent);
  35. // 4.下一个兄弟:nextElementSibling
  36. console.log(list.querySelector(":first-child + *").textContent);
  37. console.log(list.firstElementChild.nextElementSibling.textContent);
  38. // 5.最后一个兄弟:lastElementChild
  39. console.log(list.lastElementChild.textContent);
  40. // 6.前一个兄弟:previousElementSibling
  41. console.log(list.lastElementChild.previousElementSibling.textContent);
  42. // 7.父节点:parentNode
  43. console.log(list.firstElementChild.parentNode);
  44. // 8.遍历
  45. [...list.children].forEach(item => (item.style.color = "red"));
  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