Javascript appendChild onload event

前端 未结 2 1759
栀梦
栀梦 2021-01-21 08:54

I\'m appending the dynamically created image element to document.

var img = new Image();
img.src = \'test.jpg\',
img.onload = function() {

    var addedImg = co         


        
2条回答
  •  广开言路
    2021-01-21 09:26

    var img = new Image();
    var container = document.getElementsByTagName("div")[0];
    container.appendChild(img);
    
    img.onload = function() {
      alert('Width = ' + img.width);
    }
    
    img.src = "http://globe-views.com/dcim/dreams/image/image-03.jpg";
    div {
        width: 200px;
    }
    
    img {
        display: block;
        width: 100%;
        height: 100%;
    }

提交回复
热议问题