如何使用Jquery / Javascript将我的文件夹中的所有图像加载到我的网页中 [英] How to load all the images from one of my folder into my web page, using Jquery/Javascript

查看:98
本文介绍了如何使用Jquery / Javascript将我的文件夹中的所有图像加载到我的网页中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.js文件所在的目录下有一个名为images的文件夹。我想用jquery / Javascript将images文件夹中的所有图片加载到我的html页面中。

因为图像名称不是连续的整数,我怎么样应该加载这些图片?

解决方案

使用:

  var dir =Src / themes / base / images /; 
var fileextension =.png;
$ .ajax({
//这将检索文件夹的内容,如果文件夹配置为'browsable'
url:dir,
success:function(data) {
//列出页面中的所有.png文件名
$(data).find(a:contains(+ fileextension +))。each(function(){
var filename = this.href.replace(window.location.host,).replace(http://,);
$(body)。append(< img src ='+ dir + filename +'>);
});
}
});

如果您有其他扩展名,您可以将其作为一个数组,然后逐个使用 in_array()



Ps:上述源代码未经测试。

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.

Since, names of images are not some successive integers, how am I supposed to load these images?

解决方案

Use :

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

If you have other extensions, you can make it an array and then go through that one by one using in_array().

P.s : The above source code is not tested.

这篇关于如何使用Jquery / Javascript将我的文件夹中的所有图像加载到我的网页中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆