Blogger Information
Blog 19
fans 0
comment 0
visits 9861
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
访问器属性原理及应用场景、获取DOM元素的两个重要方法
newbie
Original
529 people have browsed it

访问器原理及应用场景

访问器属性就是将原本的属性伪装成方法,这样就可以直接区访问不用一级一级跳着选择

获取DOM元素的两个重要方法

1.document.querySelectorAll 获取所有元素
2.document.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>Document</title>
  8. </head>
  9. <body>
  10. <button onclick="blue()">蓝色</button>
  11. <button onclick="red()">第一个红色</button>
  12. <div class="box">box1</div>
  13. <div class="box">box2</div>
  14. <div class="box">box3</div>
  15. <div class="box">box4</div>
  16. <div class="box">box5</div>
  17. <div class="box">box6</div>
  18. <script>
  19. document.querySelectorAll(".box");
  20. const box1 = document.querySelectorAll(".box");
  21. function blue() {
  22. for (let i = 0; i < box1.length; i++) {
  23. box1[i].style.color = "blue";
  24. }
  25. }
  26. let box2 = document.querySelector(".box");
  27. function red() {
  28. box2.style.color = "red";
  29. }
  30. </script>
  31. </body>
  32. </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