Assign Javascript image object directly to page

后端 未结 3 1113
伪装坚强ぢ
伪装坚强ぢ 2021-01-26 21:58

Is it possible to assign a Javascript image object directly to the DOM? Every example I\'ve seen has the image object being loaded and then the same file name assigned to an HTM

3条回答
  •  醉话见心
    2021-01-26 22:22

    You can create the image element and append it directly to the DOM once it has loaded:

    var image = new Image();
    image.onload = function(){
        document.body.appendChild(image);
    };
    image.src = 'http://www.google.com/logos/2011/guitar11-hp-sprite.png';
    

    example: http://jsfiddle.net/H2k5W/3/

提交回复
热议问题