Como mostrar a imagem com JavaScript?

Estou a tentar mostrar a imagem, através do JavaScript, mas não consigo descobrir como fazer isso. Tenho os seguintes

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

In HTML

<p><input type="button" value="Vytvor" onclick="show_image()" > </p>
Não consigo descobrir onde devo pôr algo do género.

HTML? Ou noutro sítio qualquer...

Author: Brett DeWoody, 2011-03-27

1 answers

Você poderia fazer uso do Javascript DOM API . Em particular, olhe para o método createElement () .

Você pode criar uma função reutilizável que irá criar uma imagem como esta...

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);
}
Então podias usá-lo assim...
<button onclick=
    "show_image('http://google.com/images/logo.gif', 
                 276, 
                 110, 
                 'Google Logo');">Add Google Logo</button> 

Ver um exemplo de trabalho no jsFiddle: http://jsfiddle.net/Bc6Et/

 37
Author: jessegavin, 2013-02-14 23:08:18