【发布时间】:2019-11-18 17:08:28
【问题描述】:
我想要的很简单,每次刷新页面时都会显示一个新的图像,现在可以正常工作,我的问题是选择这些图像,有数百个,包括 .png 和 .jpg。按名称输入每个图像,甚至重命名它们似乎非常无用,如何更改我的代码以获取“图像”文件夹中的所有图像?
代码已发布。
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>✌</title>
</head>
<body> <!-- This piece of Javascript will randomly decide an image for your homepage. -->
<script>
var pictures = ['images/Aging.png', 'images/Teddy.png', 'images/Socrates.png',];
var picture = pictures[Math.floor(Math.random() * pictures.length)];
document.write('<img src="' + picture + '"/>');
</script>
</body>
</html>
【问题讨论】:
-
你不能。客户端不知道
images文件夹中有哪些文件可用,除非您公开某种可提供此信息的 api 端点。 -
获取 100 张图像只是为了选择一张来使用是非常低效的。你最好把它们打出来或重命名为你可以迭代的东西。
标签: javascript html css image random