2011-11-24 51 views
0

我正在尝试使用纯粹的JavaScript显示图像。我发现使用html和javascript工作的各种示例,但没有纯JavaScript解决方案。这里是我现在的非工作代码,仅使用javascript显示图像

<script type="text/javascript"> 
    if(document.images) 
     (new Image()).src="http://www.example.com/image.png"; 
</script> 

回答

3
var img = new Image(); 
img.src = "http://www.example.com/image.png"; 
document.body.appendChild(img); //Make sure you put the element in the DOM, otherwise it won't be visible.