How to load all the images from one of my folder into my web page, using Jquery/Javascript

后端 未结 14 2164
情深已故
情深已故 2020-11-22 10:18

I have a folder named \"images\" in the same directory as my .js file. I want to load all the images from \"images\" folder into my html page using Jquery/Javascript.

<
14条回答
  •  遥遥无期
    2020-11-22 11:00

    Based on the answer of Roko C. Buljan, I have created this method which gets images from a folder and its subfolders . This might need some error handling but works fine for a simple folder structure.

    var findImages = function(){
        var parentDir = "./Resource/materials/";
    
        var fileCrowler = function(data){
            var titlestr = $(data).filter('title').text();
            // "Directory listing for /Resource/materials/xxx"
            var thisDirectory = titlestr.slice(titlestr.indexOf('/'), titlestr.length)
    
            //List all image file names in the page
            $(data).find("a").attr("href", function (i, filename) {
                if( filename.match(/\.(jpe?g|png|gif)$/) ) { 
                    var fileNameWOExtension = filename.slice(0, filename.lastIndexOf('.'))
                    var img_html = "{2}".format(thisDirectory + filename, fileNameWOExtension, fileNameWOExtension);
                    $("#image_pane").append(img_html);
                }
                else{ 
                    $.ajax({
                        url: thisDirectory + filename,
                        success: fileCrowler
                    });
                }
            });}
    
            $.ajax({
            url: parentDir,
            success: fileCrowler
        });
    }
    

提交回复
热议问题