如果您要列出的文件在 /images 中而不是在 server.js 中,您可以添加如下内容:const express = require('express');const app = express();const path = require('path');//允许资产目录列表const serveIndex = require('serve-index');app.use('/images', serveIndex(path.join(__dirname, '/images')));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? 解决方案 Works both localhost and on live server without issues, and allows you to extend the delimited list of allowed file-extensions: var folder = "images/";$.ajax({ url : folder, success: function (data) { $(data).find("a").attr("href", function (i, val) { if( val.match(/.(jpe?g|png|gif)$/) ) { $("body").append( "<img src='"+ folder + val +"'>" ); } }); }});NOTICEApache server has Option Indexes turned on by default - if you use another server like i.e. Express for Node you could use this NPM package for the above to work: https://github.com/expressjs/serve-indexIf the files you want to get listed are in /images than inside your server.js you could add something like:const express = require('express');const app = express();const path = require('path');// Allow assets directory listingsconst serveIndex = require('serve-index'); app.use('/images', serveIndex(path.join(__dirname, '/images'))); 这篇关于如何使用 Jquery/Javascript 将我的一个文件夹中的所有图像加载到我的网页中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 20:45