Blogger Information
Blog 7
fans 1
comment 0
visits 3961
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
自定义方法实现通过类名获取所有元素的对象集合
Lucas
Original
482 people have browsed it

通过类名获取所有元素的方法

页面代码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>自定义方法,实现通过类名获取对象集合</title>
  6. </head>
  7. <body>
  8. <h1 id='one'>自定义方法,实现通过类名获取对象集合</h1>
  9. <h2 class='one'>自定义方法,实现通过类名获取对象集合</h2>
  10. <p class='one'>自定义方法,实现通过类名获取对象集合</p>
  11. <div class='two one'>自定义方法,实现通过类名获取对象集合</div>
  12. <h2 class='two'>自定义方法,实现通过类名获取对象集合</h2>
  13. <h2 class='tree one'>自定义方法,实现通过类名获取对象集合</h2>
  14. <h2 class='tree two'>自定义方法,实现通过类名获取对象集合</h2>
  15. <script>
  16. var x=document.getElementsByClassName('one'); //x是一个对象集合的数组
  17. for(i = 0; i < x.length; i++){
  18. x[i].style.background='green';
  19. x[i].style.color = 'yellow'
  20. }
  21. console.log(x)
  22. </script>
  23. </body>
  24. </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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!