ASCII码 ASCII码

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

发布于:2021-12-10 08:57:21  栏目:技术文档

图片展示

代码展示

  1. <div class="one">我是one</div>
  2. <div class="wrap">
  3. <div class="one">我也是one</div>
  4. <div class="two">我也two</div>
  5. <div class="one">i'm one too</div>
  6. </div>
  7. <script>
  8. function selectorClass(className) {
  9. if (className.indexOf('.') === 0) {
  10. className = className.slice(1);
  11. }
  12. return document.getElementsByClassName(className);
  13. }
  14. var oneObjArr = selectorClass('.one');
  15. console.log(oneObjArr);
  16. for (var i in oneObjArr) {
  17. oneObjArr[i].innerHTML = '我们都是one'
  18. }
  19. </script>
相关推荐
阅读 +