我正在创建一个按钮,每次单击按钮后状态栏都需要更新以显示“您已生成数字 x 次”,其中 x 是自页面加载以来按钮被单击的次数。我
这是我到目前为止的全部代码(我正在制作一个随机数生成器):
<!DOCTYPE html>
<html>
<body>
<font face="Times New Roman" style="color:MediumOrchid">
<h1 align="left"> Play At Your Own Risk! </h1>
</font>
<button onclick="randomNum ()"> Please click the button to roll the dice </button>
<p id="demo"> </p>
<script>
function randomNum ()
{
var listOfNumbers=[];
var stringOutput="";
for (i=0;i<5;i++) {
listOfNumbers.push(Math.floor(Math.random()*59)+1);
stringOutput += listOfNumbers[i] + "";
}
document.getElementById("demo").innerHTML = stringOutput;
}
</script>
</body>
</html>
最佳答案
您可以创建一个全局变量来存储点击次数。在按钮的事件处理程序中显示当前点击并递增。 JQuery 是你的 friend :)
关于javascript - 如何让按钮显示它在 javascript 中被点击的次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33877783/