如何使图像显示在Javascript中 [英] How to make images appear in Javascript

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

问题描述

我不是非常流利的javascript,我觉得这是非常基本的,但我似乎无法在网上找到任何地方

I'm not very fluent in javascript, and I feel like this is really basic, but I can't seem to find it online anywhere.

我想创建一个链接,触发一个javascript函数,使图像出现在一个单独的div中。它不能在flash中,否则我不反对编码语言。

I want to create a link that will trigger a javascript function that makes an image appear in a separate div. It can't be in flash, otherwise I have no objection to the coding language.

我有几张图片,所以我认为最好的方法是将它们全部叠加在一起,然后在每次点击链接时增加z-index,但你可能有更好的想法。

I have several images, so I would think that the best way to do this would be to layer them all on top of each other and then increase the z-index each time their link is clicked, but you might have a better idea.

我真的只是想要创建一种不使用flash的原始图库,并在点击相应的链接时在其div中显示照片。

I really just want someway to create a sort of primitive image gallery that doesn't use flash and displays the photo in its div when the corresponding link is clicked.

提前谢谢,Alex

推荐答案

正如Jessegavin所说(发现这里

As Jessegavin said (found here)

您可以使用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> 

上面不应该有换行符,我添加它以便它可以
无滚动显示请参阅jsFiddle的工作示例:
http://jsfiddle.net/Bc6Et/

There shouldn't be a line break above, I added it so that it could show without scrolling See a working example on jsFiddle: http://jsfiddle.net/Bc6Et/

这应该回答你的问题

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

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