Home >Web Front-end >JS Tutorial >How to Dynamically Load Images from a Folder into a Web Page Using jQuery/JavaScript?
Question:
How can we dynamically load all images from a specific folder into an HTML page using jQuery/JavaScript, even when the image names are not sequential?
Answer:
To achieve this, we can utilize AJAX and a clever technique to identify image files within the desired folder.
<code class="javascript">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 + "'>"); } }); }, });</code>
Explanation:
Note:
The above is the detailed content of How to Dynamically Load Images from a Folder into a Web Page Using jQuery/JavaScript?. For more information, please follow other related articles on the PHP Chinese website!