最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - How to load images from folder by using javascript - Stack Overflow

programmeradmin3浏览0评论

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

} 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);

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

} 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);

Share Improve this question asked Dec 12, 2015 at 11:07 Yaroslav ShabanovYaroslav Shabanov 2831 gold badge3 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 1

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.

发布评论

评论列表(0)

  1. 暂无评论