我正在尝试通过JavaScript显示图像,但我无法弄清楚如何做到这一点。我有以下
function image(a,b,c)
{
this.link=a;
this.alt=b;
this.thumb=c;
}
function show_image()
{
document.write("img src="+this.link+">");
}
image1=new image("img/img1.jpg","dsfdsfdsfds","thumb/img3");
HTML中的
<p><input type="button" value="Vytvor" onclick="show_image()" > </p>
我无法弄清楚我应该在哪里放置image1.show_image();
。
HTML?或者其他地方......
答案 0 :(得分:40)
您可以使用Javascript DOM API。特别是,请查看createElement()方法。
你可以创建一个可重复使用的功能,它将创建一个像这样的图像......
function show_image(src, width, height, alt) {
var img = document.createElement("img");
img.src = src;
img.width = width;
img.height = height;
img.alt = alt;
// This next line will just add it to the <body> tag
document.body.appendChild(img);
}
然后你可以像这样使用它......
<button onclick=
"show_image('http://google.com/images/logo.gif',
276,
110,
'Google Logo');">Add Google Logo</button>