Insert image object into HTML

后端 未结 2 1916
执笔经年
执笔经年 2020-12-24 07:51

I\'m just curious to know if there\'s a better solution for doing this:

var img = new Image();
var div = document.getElementById(\'foo\');

img.onload = func         


        
2条回答
  •  时光说笑
    2020-12-24 08:38

    You can use this way:

    var img = new Image();
    var div = document.getElementById('theDiv');
    
    img.onload = function() {
      div.appendChild(img);
    };
    
    img.src = 'path/to/image.jpg';
    

提交回复
热议问题