ES6 中用class创建img 详细明了,步骤清晰,解释完美,谢谢欣赏

<body>
<div id="box">

</body>

<script type="text/javascript">

    class A {
      constructor() {
      this.box = document.getElementById("box");  //获取父节点
      this.img = document.createElement('img')      //创建元素
      this.addEvent()
    }
      addEvent(){
      // this.img.setAttribute('src', '../images/1.jpg')   //两种方法都可与
      this.img.src = '../images/1.jpg';            //设置图片的路径
      this.add();
    }
      add(){
      this.box.appendChild(this.img);      //插入节点
    }
  }
  new A;
</script>

原文地址:https://www.cnblogs.com/CH-cnblogs/p/13386396.html