Blogger Information
Blog 18
fans 0
comment 0
visits 8469
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
获取DOM元素常见API和遍历方法
时间在渗透
Original
431 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>获取DOM元素常见API和遍历方法</title>
  8. </head>
  9. <body>
  10. <h3>1. 获取元素的</h3>
  11. <ul>
  12. <li>item-1</li>
  13. <li>item-2</li>
  14. <li>item-3</li>
  15. <li>item-4</li>
  16. <li>item-5</li>
  17. </ul>
  18. <h3> 2. dom常用的遍历方法</h3>
  19. <script>
  20. // 1. 获取元素文本
  21. console.log("获取第1个元素的文本: \n" + document.querySelector("ul>li:first-child").textContent);
  22. console.log("获取第3个元素的文本: \n" + document.querySelector("ul>li:nth-of-type(3)").textContent);
  23. console.log("获取最后一个元素的文本: \n" + document.querySelector("ul>li:last-of-type").textContent);
  24. // 2. 获取子节点
  25. let arr = document.querySelector('ul');
  26. console.log("获取childNodes节点: \n", arr.childNodes);
  27. // 获取所有的元素节点
  28. console.log("获取children节点: \n", arr.children);
  29. // 遍历
  30. console.log("遍历节点: \n",);
  31. // 遍历的每个li 的颜色设置为蓝色
  32. [...arr.children].forEach(function (li) {
  33. li.style.color = 'blue';
  34. console.log('遍历的li:', li.textContent)
  35. });
  36. </script>
  37. </body>
  38. </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