实现预加载一张图片,加载完成后显示在网页中,并设定其高度为50px;宽度为50px。解决方法如下所示:

var imgObj = document.getElementById('imgObg');
addImg('5.png');
function addImg(isrc) {
    imgObj1 = new Image();
    imgObj1.src = isrc;
    imgObj1.onload = function() {
        imgObj.src = this.src;
        imgObj.height = 50;
        imgObj.width = 50;
    }
}

知识点:预加载事件为onload,加载成功后为图片设置样式。