使用javascript将图像添加到HTML文档 [英] Adding images to an HTML document with javascript

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

问题描述

我已经从多个站点尝试了一些HTML DOM代码,但是它不起作用。它没有添加任何东西。有人在这方面有可行的例子吗?

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)

但它并未添加任何内容div。 (gamediv)我也尝试了document.body,但没有结果。

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

预先感谢。

推荐答案

您需要使用<$第3行中的c $ c> document.getElementById()。

如果您现在在控制台中尝试以下操作:

If you try this right now in the 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);

<div id="header"></div>

...你会得到这个:

... you'd get this:

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

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