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

时间:2022-06-02 05:53:07

There are 0.png, 1.png, 2.png, ... in folder images. How to load all them. Number of the images unknown.

文件夹图像中有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);

1 个解决方案

#1


0  

Code:

    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 + "'>");
        });
    }
});

This will load all the image .png present in a folder, care that this code use jquery.

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

#1


0  

Code:

    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 + "'>");
        });
    }
});

This will load all the image .png present in a folder, care that this code use jquery.

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