如何使用 JavaScript 显示图像? [英] How to display image with JavaScript?

查看:24
本文介绍了如何使用 JavaScript 显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 JavaScript 显示图像,但我不知道该怎么做.我有以下

I am trying to display image, through JavaScript, but i can't figure out how to do that. I have following

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();之类的东西放在哪里.

I can't figure out where should I put something like image1.show_image();.

HTML?或者其他地方...

HTML? Or somewhere else...

推荐答案

您可以使用 Javascript DOM API.特别是,请查看 createElement() 方法.

You could make use of the Javascript DOM API. In particular, look at the createElement() method.

您可以创建一个可重复使用的函数来创建像这样的图像...

You could create a re-usable function that will create an image like so...

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);
}

然后你可以这样使用它...

Then you could use it like this...

<button onclick=
    "show_image('http://google.com/images/logo.gif', 
                 276, 
                 110, 
                 'Google Logo');">Add Google Logo</button> 

这篇关于如何使用 JavaScript 显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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