简体   繁体   English

如何在不指定顺序名称的情况下通过 Javascript 将目录中的图像添加到 HTML 元素

[英]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.

相关问题 删除HTML标记而不用JavaScript指定名称 - Remove HTML tags without specifying names by JavaScript 如何在没有javascript而没有jquery的情况下将id添加到没有id的元素 - how can I add an html to an element without id with javascript without jquery 如何将类添加到 Html 或 Javascript 中的元素? - How to add class to an element in Html or via Javascript? 如何解决此问题,以将HTML div元素中的图像添加到Google地图标记弹出窗口内容? - How can I fix this to add images from HTML div element to google map marker popup content? 如何通过jQuery或Javascript添加具有绝对位置的元素? - How can I add an element with absolute position via jQuery or Javascript? 如何遍历以下充满图像的目录并将图像添加到我的 HTML 页面 - How can I iterate through the following directory full of images and add the images to my HTML page 如何在不知道ID的情况下从JavaScript中选择HTML元素? - How can I select an HTML element from JavaScript without knowing its ID? 如何向这个 HTML 元素添加两个 JavaScript 函数 - How can I add two JavaScript functions to this HTML element 在Javascript中如何在不指定rgb的情况下设置rgba? - In Javascript how can I set rgba without specifying the rgb? 如何配置节点包,以便在不指定src目录的情况下加载源? - How can I configure my node package so I can load sources without specifying the src directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM