如何将图片附加到阵列的位置?javascript

如何将图片附加到阵列的位置?javascript,javascript,arrays,image,Javascript,Arrays,Image,我正在创建一个脚本,当按下一个按钮时可以显示5个图像。 我需要在数组的位置插入5个图像,类似于 array [0] = image1; array [1] = image2; etc ... 当用户按下“更改”按钮时,图像1(显示在屏幕上)将替换为附加到阵列第二位置的第二个图像 <style type="text/css"> #button1 { border:2px #333 solid; padding:20px; width:250px; -webkit-bo

我正在创建一个脚本,当按下一个按钮时可以显示5个图像。 我需要在数组的位置插入5个图像,类似于

array [0] = image1; 
array [1] = image2; 

etc ... 
当用户按下“更改”按钮时,图像1(显示在屏幕上)将替换为附加到阵列第二位置的第二个图像

<style type="text/css">
#button1 
{
border:2px #333 solid;   
padding:20px;
width:250px;
-webkit-border-radius:40px;
-moz-border-radius:40px;
border-radius:40px;
}
</style>
<div align="center">
<br><br><br><br>
<img src="Koala.jpg" id="im1" width="300px" height="200px">
<img src="Casa.jpg" id="im1" width="300px" height="200px">
<img src="Tulipani.jpg" id="im1" width="300px" height="200px">
<img src="Pinguini.jpg" id="im1" width="300px" height="200px">
<img src="Deserto.jpg" id="im1" width="300px" height="200px">
<br><br>
<input type="button" id="button1" value="cambia" onclick="im()" height="50">
</div>
<script>

var array = new Array();

function im()
{
console.log("on");
}

</script> 

#按钮1
{
边框:2px#333实心;
填充:20px;
宽度:250px;
-webkit边界半径:40px;
-moz边界半径:40px;
边界半径:40px;
}






var数组=新数组(); 函数im() { 控制台。登录(“登录”); }
但此代码显示所有5个图像

直到现在我才意识到html样式,但我不知道有什么方法或属性可以让我继续。你能帮我吗?

你能做到 数组[0]=$('#im1').attr('src'); 数组[1]=$('#im2').attr('src'); 并在html中更改图片的id

<img src="Deserto.jpg" id="im1" width="300px" height="200px">
<!-- only one <img>. And btw. never use the same id multiple times -->
<br><br>
<input type="button" id="button1" value="cambia" onclick="im()" height="50">
你可以这样做 数组[0]=$('#im1').attr('src'); 数组[1]=$('#im2').attr('src'); 并在html中更改图片的id

<img src="Deserto.jpg" id="im1" width="300px" height="200px">
<!-- only one <img>. And btw. never use the same id multiple times -->
<br><br>
<input type="button" id="button1" value="cambia" onclick="im()" height="50">
你可以这样做 数组[0]=$('#im1').attr('src'); 数组[1]=$('#im2').attr('src'); 并在html中更改图片的id

<img src="Deserto.jpg" id="im1" width="300px" height="200px">
<!-- only one <img>. And btw. never use the same id multiple times -->
<br><br>
<input type="button" id="button1" value="cambia" onclick="im()" height="50">
你可以这样做 数组[0]=$('#im1').attr('src'); 数组[1]=$('#im2').attr('src'); 并在html中更改图片的id

<img src="Deserto.jpg" id="im1" width="300px" height="200px">
<!-- only one <img>. And btw. never use the same id multiple times -->
<br><br>
<input type="button" id="button1" value="cambia" onclick="im()" height="50">

这里有一把供您使用的小提琴:

基本上,您希望使用以下函数依次在每张图片上切换类
current
。使用以下HTML(注意,我在第一个img中添加了
current
类)

然后,您需要做的就是添加一点css,使当前的
类显示出来,而所有其他图像都隐藏起来:

function im() {
  this.imgs = this.imgs || document.querySelectorAll("div > img");
  var l = this.imgs.length,
      img;

  this.i = !this.i ? 1 : (this.i < l ? this.i : 0);

  for(;img = imgs[--l];)
    img.classList.remove('current');

  imgs[this.i++].classList.add('current');
}

这里有一把供你使用的小提琴:

基本上,您希望使用以下函数依次在每张图片上切换类
current
。使用以下HTML(注意,我在第一个img中添加了
current
类)

然后,您需要做的就是添加一点css,使当前的
类显示出来,而所有其他图像都隐藏起来:

function im() {
  this.imgs = this.imgs || document.querySelectorAll("div > img");
  var l = this.imgs.length,
      img;

  this.i = !this.i ? 1 : (this.i < l ? this.i : 0);

  for(;img = imgs[--l];)
    img.classList.remove('current');

  imgs[this.i++].classList.add('current');
}

这里有一把供你使用的小提琴:

基本上,您希望使用以下函数依次在每张图片上切换类
current
。使用以下HTML(注意,我在第一个img中添加了
current
类)

然后,您需要做的就是添加一点css,使当前的
类显示出来,而所有其他图像都隐藏起来:

function im() {
  this.imgs = this.imgs || document.querySelectorAll("div > img");
  var l = this.imgs.length,
      img;

  this.i = !this.i ? 1 : (this.i < l ? this.i : 0);

  for(;img = imgs[--l];)
    img.classList.remove('current');

  imgs[this.i++].classList.add('current');
}

这里有一把供你使用的小提琴:

基本上,您希望使用以下函数依次在每张图片上切换类
current
。使用以下HTML(注意,我在第一个img中添加了
current
类)

然后,您需要做的就是添加一点css,使当前的
类显示出来,而所有其他图像都隐藏起来:

function im() {
  this.imgs = this.imgs || document.querySelectorAll("div > img");
  var l = this.imgs.length,
      img;

  this.i = !this.i ? 1 : (this.i < l ? this.i : 0);

  for(;img = imgs[--l];)
    img.classList.remove('current');

  imgs[this.i++].classList.add('current');
}

你的HTML是什么?您需要一个计数器变量(
i
),每次单击时,您都会将图像的
src
属性更改为
数组[++i]
中指定的URI(不过请注意边界!)您的HTML是什么?您需要一个计数器变量(
i
),每次单击时,您都会将图像的
src
属性更改为
数组[++i]
中指定的URI(不过请注意边界!)您的HTML是什么?您需要一个计数器变量(
i
),每次单击时,您都会将图像的
src
属性更改为
数组[++i]
中指定的URI(不过请注意边界!)您的HTML是什么?您需要一个计数器变量(
i
),每次单击时,您都会将图像的
src
属性更改为
数组[++i]
中指定的URI(注意边界!)这是一个非常不可思议的解决方案。这是一个非常不可思议的解决方案。这是一个非常不可思议的解决方案。这是一个非常不可思议的解决方案。