从一个数组HTML JavaScript图片 [英] Image from an array HTML Javascript

查看:753
本文介绍了从一个数组HTML JavaScript图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个基本的问题,但大多数的例子,我觉得是有点太复杂了。

This is a basic question but most examples I find are a little bit too complex.

我想创建一个照片阵列,然后只显示所选择的一个。

I am trying to create an array of photos, and then display only a selected one.

这是我的数组:

var imgArray = new Array();
imgArray[0] = new Image();
imgArray[0].src = 'images/pie1a.png';
imgArray[1] = new Image();
imgArray[1].src = 'images/pie1b.png';
imgArray[2] = new Image();
imgArray[2].src = 'images/pie2a.png';
imgArray[3] = new Image();
imgArray[3].src = 'images/pie2b.png';

这是我的HTML:

<img src=imgArray[1] width="33" height="32">

我也试过这样的:

I also tried this:

document.write('<img src="' + imgArray[1] + '" width="120" height="120"/>');    

永远不会显示照片。
我在做什么错了?

The photo is never displayed. What am I doing wrong?

推荐答案

试试这个:

var imgArray = [
    'images/pie1a.png',
    'imgaes/pie1b.png',
    // ...
];
document.write('<img src="'+imgArray[1]+'" width="120" height="120" />');

请注意,文件撰写是不是一个好主意,它应该是可以避免的,但如果它至少得到您的code工作这是一个良好的开端!

Note that document.write is not a good idea, it should be avoided, but if it at least gets your code working it's a good start!

这篇关于从一个数组HTML JavaScript图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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