1

That's all I want to do. I'm very beginner in javascript but I just don't understand why this wouldn't work. I've looked across the depths of google but people are asking more complicated things. I literally just want to show an image I've stored in an array.

$(document).ready(function(){
    var coolImage = new Array();

    coolImage[0] = "images/2.gif";

    var img = new Image();
    img.src = coolImage[0];

    document.write(img);
});

All I get on the page is [object HTMLImageElement].

4

3 回答 3

5

方括号和对象名称表明您正在将 Image 对象的字符串表示形式写入页面。尝试替换document.write(img);为:

document.body.appendChild(img);

可以在这里看到一个例子

于 2013-08-23T00:45:20.120 回答
1

您正在使用 JavaScript Image 对象,但您需要一个 Image 元素:

var image = document.createElement("img");
image.src = coolImage[0];
document.body.appendChild(image);
于 2013-08-23T00:59:12.167 回答
0

如果您使用的是 jQuery,则可以使用:

$('body').append(img);

于 2013-08-23T00:50:23.227 回答