Blogger Information
Blog 70
fans 1
comment 0
visits 52933
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
自定义方法通过类名获取对象集合
葡萄枝子
Original
598 people have browsed it

自定义方法通过类名获取对象集合

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <p class="test test1">class -> test test1</p>
  9. <p class="test test1 test2">class -> test test1 test2</p>
  10. <p class="test test2">class -> test test2</p>
  11. <script>
  12. function classSelectors(className, tagName) {
  13. // return [...document.getElementsByTagName(tagName || '*')].filter(function (value) {
  14. // return className.split(' ').every(function (elem) {
  15. // return value.className.split(' ').includes(elem)
  16. // })
  17. // });
  18. return [...document.getElementsByTagName(tagName || '*')].filter(value => className.split(' ').every(elem => value.className.split(' ').includes(elem)));
  19. }
  20. // 获取首个添加背景和字体颜色测试
  21. classSelectors('test test2', 'p').shift().style.cssText = 'background:yellow;color:red';
  22. console.log(classSelectors('test test2', 'p'));
  23. </script>
  24. </body>
  25. </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