[英]How can I add images from a directory via Javascript to an HTML element without specifying sequential names
I need to generate a list with images from a given folder in my HTML via Javascript.我需要通过 Javascript 从我的 HTML 中的给定文件夹中生成一个包含图像的列表。
Okay, I did that, but after searching dozens of similar codes, I realized that it can only add images with sequential names (for example, 01.jpg, 02.jpg, 03.jpg ...) and I need to load all the images without specifying the names , so that I don't have to rename them.
好吧,我这样做了,但是在搜索了几十个类似的代码后,我意识到它只能添加具有顺序名称的图像(例如,01.jpg、02.jpg、03.jpg ...),我需要加载所有图像没有指定名称,这样我就不必重命名它们。
This is my code:这是我的代码:
for (i = 01; i <= 09; i++) { document.write('<li class="slider"><img src="https://jar.is/cdn/img/0'+i+'.jpg" alt="'+i+'" title="'+i+'" /></li>'); }
.slider { list-style: none; padding-left: 0; display: flex; flex-flow: row wrap; margin: 3px; align-items: center; justify-content: center; } .slider img { max-width: 450px; width: 270px; height: 210px; object-fit: cover; border-radius: 6px; }
<ul class="slider"></ul>
Is there a way to load ALL THE IMAGES (*.jpg) in my HTML list?有没有办法在我的 HTML 列表中加载所有图像 (*.jpg)?
Thanks a lot.非常感谢。
If it's not a Server side JavaScript, there is no simple way to get the complete folder contents from the client side.如果它不是服务器端 JavaScript,则没有简单的方法可以从客户端获取完整的文件夹内容。 You would need to send the file list from your backend or get it by AJAX via some kind of API.
您需要从后端发送文件列表或通过某种 API 由 AJAX 获取它。
See Get list of filenames in folder with Javascript for more inspiration.有关更多灵感,请参阅使用 Javascript 获取文件夹中的文件名列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.