javascript 如何使用javascript从文件夹中加载图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34239130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 17:25:38 来源:igfitidea点击:
How to load images from folder by using javascript
提问by Yaroslav Shabanov
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);
回答by master94ga
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。