gpt4 book ai didi

javascript - 如何让这个 HTML/Javascript 随机数生成器输出多个结果?

转载 作者:行者123 更新时间:2023-12-01 02:32:26 25 4
gpt4 key购买 nike

预先感谢您提供的任何帮助。我有一个简单的加载随机数生成器,但遇到问题。

基本上我试图在一些文本之间插入生成的数字。我运气不好,但似乎我已经尝试了一切。它总是在短语之间划一条线。

我的第二个问题是我不知道如何输出多个随机数。我是 JavaScript 新手,所以任何解释将不胜感激!

function getNumber() {
var minNumber = 10; // The minimum number you want
var maxNumber = 10; // The maximum number you want
var randomnumber = Math.floor(Math.random() * (maxNumber + 1) + minNumber);
document.getElementById("myNumber").innerHTML = randomnumber;
// Sets content of <div> to number
return false;
}
window.onload = getNumber;
<div id="myNumber"> </div>
<p id="yourNumber"> </p>

I am (random number) years old.
My brother is (random number) years old.
My dog had (random number) puppies last year.

最佳答案

我正在使用输出元素,因为它应该用于结果......我使用querySelectorAll获取所有输出元素。

document.addEventListener("DOMContentLoaded", function(event) {
function getNumber() {
output => output.value = getNumber();
var minNumber = 10; // The minimum number you want
var maxNumber = 20; // The maximum number you want
return Math.floor(Math.random() * (maxNumber + 1) + minNumber);
}

// Using querySelectorAll to get all output-elements, and then update the value of the output-element with a random number.
document.querySelectorAll('output').forEach(
output => output.value = getNumber()
);
});
<div>I am <output name="me"></output> years old.</div>
<div>My brother is <output name="brother"></output> years old. </div>
<div>My dog had <output name="dog"></output> puppies last year. </div>

关于javascript - 如何让这个 HTML/Javascript 随机数生成器输出多个结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48218615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com