我对某件事有疑问。所以我写了下面的代码,但遇到了问题。我想要发生的是在计算机生成随机数(在脚本中)之后,我希望该数字在敏捷之后立即显示。抱歉,这很模糊,但我不确定如何解释我想要的内容。
<html>
<body>
<h4> Agility </h4>
<script>
function rollAFunction() {
document.getElementById("diceARoll").innerHTML = Math.floor(Math.random() * 20 + 1)
}
</script>
</body>
</html>
最佳答案
只需在 Agility 之后添加一个带有您要访问的 id 的跨度,然后调用函数来填充它即可。
<html>
<body>
<h4> Agility <span id="diceARoll"></span></h4>
<script>
function rollAFunction() {
document.getElementById("diceARoll").innerHTML = Math.floor(Math.random() * 20 + 1)
}
rollAFunction();
</script>
</body>
关于JavaScript 显示数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32428886/