我制作了一个随机数生成器,我正在尝试将这些值添加到列表中,以便随后我可以将它们再次删除,因为“这些是最后十个数字”到目前为止,我有这个:
<html>
<head></head>
<body>
<h1>
The last number was:
<p id="userData"></p>
</h1>
<button onclick="random()">Random</button>
<script>
function random() {
document.getElementById("userData").innerHTML = Math.round(Math.random() * 100000000);
}
</script>
</body>
</html>
我很乐意得到任何帮助。谢谢
您可以使用基本的javascript数组来保存您计算出的随机值。
randoms = []; // Create an array
function random() {
let newRandom = Math.round(Math.random() * 100000000);
randoms.push(newRandom); // save newly created number to that array
document.getElementById("userData").innerHTML = newRandom;
}
在randoms
中,您现在具有所有计算出的随机数的列表,以供进一步使用。
为了将这些值呈现到html页,可以更改随机函数并添加document.getElementById("randoms").innerHTML = randoms;
。将div <div id="randoms"></div>
添加到html页面时,这将导致所有随机值打印到该页面。