首页 > 技术文章 > HTMLCollection

Azune 2020-12-11 09:12 原文

问题:需要给动态生成的每一个img添加事件

 let List = document.getElementsByTagName("img");
 // 此处获取回来的就是Collection数组,不可以直接使用数组的方法,必须转化使用
      let Arr = Array.from(List);
      for (var i = 0; i < Arr.length; i++) {
        Arr[i].onclick = function() {
          _this.showImg = true;
          _this.imgSrc = this.src;
        };
   }

推荐阅读