【问题标题】:How to load images from folder by using javascript如何使用javascript从文件夹中加载图像
【发布时间】:2016-03-18 06:52:51
【问题描述】:

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

【问题讨论】:

    标签: javascript arrays image load directory


    【解决方案1】:

    代码:

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

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

    【讨论】:

    猜你喜欢
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 2012-07-28
    • 2012-08-24
    • 1970-01-01
    相关资源
    最近更新 更多