如何使用javascript从文件夹加载图像

How to load images from folder by using javascript

本文关键字:加载 图像 文件夹 何使用 javascript      更新时间:2023-09-26

文件夹图像中有0.png、1.png、2.png。如何加载所有这些。图像数量未知。

} while(!img[numOfImages].onerror);
alert("numOfImages = " + numOfImages);
var	
  numOfImages = 0;
  img = [];	
do{
	img[numOfImages] = new Image();
	img[numOfImages].src = "images/" + numOfImages + ".png";
	numOfImages++;
} while(!img[numOfImages].onerror);
alert("numOfImages= " + numOfImages);

代码:

    var dir = "Src/themes/base/images/";
var fileextension = ".png";
$.ajax({
    //This will retrieve the contents of the folder if the folder is configured as 'browsable'
    url: dir,
    success: function (data) {
        //List all .png file names in the page
        $(data).find("a:contains(" + fileextension + ")").each(function () {
            var filename = this.href.replace(window.location.host, "").replace("http://", "");
            $("body").append("<img src='" + dir + filename + "'>");
        });
    }
});

这将加载文件夹中的所有image.png,请注意此代码使用jquery。