使用javascript添加图像到html [英] Adding images to the html with javascript

查看:122
本文介绍了使用javascript添加图像到html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从几个网站尝试过一些HTML DOM代码,但是它不起作用。没有添加任何东西有没有人有这个工作的例子?

  this.img = document.createElement(img); 
this.img.src =img / eqp /+ this.apparel +/+ this.facing +_ idle.png;
src = getElementById(gamediv);
src.appendChild(this.img)

但是没有添加任何内容DIV。 (gamediv)我也尝试过document.body,没有结果。



提前感谢

解决方案

您需要使用 document.getElementById()在第3行。



如果您现在在Firebug控制台中尝试这样:

  var img = document.createElement(img); 
img.src =http://www.google.com/intl/en_com/images/logo_plain.png;

var src = document.getElementById(header);
src.appendChild(img);

...你会得到这样的:



使用JavaScript将图像添加到HTML http://img94.imageshack.us/img94/3769/googso2。 png


I've tried some HTML DOM code from several sites, but it isn't working. It isn't adding anything. Does anyone have a working example on this?

this.img = document.createElement("img");
this.img.src = "img/eqp/"+this.apparel+"/"+this.facing+"_idle.png";
src = getElementById("gamediv");
src.appendChild(this.img)

But it isn't adding anything to the div. (gamediv) I've tried document.body as well, with no result.

Thanks in advance.

解决方案

You need to use document.getElementById() in line 3.

If you try this right now in the Firebug console:

var img = document.createElement("img");
img.src = "http://www.google.com/intl/en_com/images/logo_plain.png";

var src = document.getElementById("header");
src.appendChild(img);

... you'd get this:

Adding images to the HTML with JavaScript http://img94.imageshack.us/img94/3769/googso2.png

这篇关于使用javascript添加图像到html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆