简体   繁体   English

通过循环动态数组元素

[英]dynamic array elements through loop

I am trying to create an array of images in javascript for my website when I declare array size it is working but when I am trying to take it as a dynamic array it is not showing images.当我声明数组大小它正在工作时,我试图用javascript为我的网站创建一个图像数组,但是当我尝试将它作为动态数组时,它不显示图像。 can anyone tell me what's wrong with this code?谁能告诉我这段代码有什么问题?

var theImages = new Array()
for (i = 0; i < theImages.length; i++) 
{  
  theImages[i] = i+".jpg"
}

The initial size of theImages is 0. theImages 的初始大小为 0。

You need to use你需要使用

for (i = 0; i < 5; i++) 
{  
  theImages[i] = i+".jpg";
}

replace 5 by the number of images you have.用您拥有的图像数量替换 5。

This is happening because you are looping an empty array, the length/size of the array is 0 because you have just created a new array and are looping it without any elements in it yet.发生这种情况是因为您正在循环一个空数组,该数组的长度/大小为 0,因为您刚刚创建了一个新数组并且正在循环它,其中还没有任何元素。

If you wish to add all images to an array, you will have to know the total number of images you have/ count of images, and then run the loop to add the images in the array, which you were able to do successfully you said.如果您希望将所有图像添加到一个数组中,您必须知道您拥有的图像总数/图像数,然后运行循环以将图像添加到数组中,您可以成功完成您所说的.

var theImages = new Array();
for (i = 0; i < 6; i++) 
{  
theImages[i] = i+".jpg";
}

If you are getting the image names for SQL, it is a different query you want to use, let me know if that is what you are looking for.如果您正在获取 SQL 的图像名称,那么您要使用的是不同的查询,如果这就是您要查找的内容,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM