嗨,我是 html 新手,我只是为了好玩而做一些练习。我正在尝试以 html 格式显示多个图像,但不知道如何显示。例如,
<img src="/images/1/1.jpg" class="center">
<img src="/images/1/2.jpg" class="center">
<img src="/images/1/3.jpg" class="center">
<img src="/images/1/4.jpg" class="center">
我现在有这个,但我不想每次都写这个。我想知道我该怎么做。我可以使用 JavaScript 或 php 或其他任何东西将所有图像一次放入文件夹中吗?
最佳答案
PHP 上的简单代码:
<?php
$refFolder = "/images/1/";
function folderList($Path) {
return array_slice(scandir($Path,SCANDIR_SORT_ASCENDING), 0);
}
foreach(folderList($refFolder) as $file) {
$Z_info = pathinfo($file, PATHINFO_FILENAME);
$Z_type = pathinfo($file, PATHINFO_EXTENSION);
if ($Z_type == 'jpg') {
?>
<img class="center"
src="<? echo $refFolder.$file; ?>"
alt="<? echo $Z_info; ?>"
/>
<?php } } ?>
关于javascript - 显示文件夹中的多个 html 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59260211/