我试图在一个页面上添加大约20个单独的随机生成的55到999之间的数字字符串,这可能吗?我尝试将元素ID从“randomnumber”附加到randomnumber2,3,4,5等......“没有成功。有什么想法吗?
我尝试更改此脚本以满足我的需求,但它似乎不会多次使用。
<head>
<script type="text/javascript">
window.onload = generateRandomNumber;
function generateRandomNumber(){
var n = 999;
var number = Math.floor(Math.random()*n)+1;
document.getElementById("randomNumber").innerHTML = number;
}
</script>
</head>
<body>
<class id="randomNumber"></class>
</body>
答案 0 :(得分:0)
解决了:
<head>
<script>
window.onload = generateRandomNumber;
var n = 4000;
var number = Math.floor(Math.random()*n)+1;
var number2 = Math.floor(Math.random()*n)+1;
var number3 = Math.floor(Math.random()*n)+1;
var number4 = Math.floor(Math.random()*n)+1;
var number5 = Math.floor(Math.random()*n)+1;
var number6 = Math.floor(Math.random()*n)+1;
var number7 = Math.floor(Math.random()*n)+1;
var number8 = Math.floor(Math.random()*n)+1;
var number9 = Math.floor(Math.random()*n)+1;
var number10 = Math.floor(Math.random()*n)+1;
var number11 = Math.floor(Math.random()*n)+1;
var number12 = Math.floor(Math.random()*n)+1;
var number13 = Math.floor(Math.random()*n)+1;
var number14 = Math.floor(Math.random()*n)+1;
var number15 = Math.floor(Math.random()*n)+1;
var number16 = Math.floor(Math.random()*n)+1;
var number17 = Math.floor(Math.random()*n)+1;
var number18 = Math.floor(Math.random()*n)+1;
function generateRandomNumber(){
document.getElementById("randomNumber").innerHTML = number;
document.getElementById("randomNumber2").innerHTML = number2;
document.getElementById("randomNumber3").innerHTML = number3;
document.getElementById("randomNumber4").innerHTML = number4;
document.getElementById("randomNumber5").innerHTML = number5;
document.getElementById("randomNumber6").innerHTML = number6;
document.getElementById("randomNumber7").innerHTML = number7;
document.getElementById("randomNumber8").innerHTML = number8;
document.getElementById("randomNumber9").innerHTML = number9;
document.getElementById("randomNumber10").innerHTML = number10;
document.getElementById("randomNumber11").innerHTML = number11;
document.getElementById("randomNumber12").innerHTML = number12;
document.getElementById("randomNumber13").innerHTML = number13;
document.getElementById("randomNumber14").innerHTML = number14;
document.getElementById("randomNumber15").innerHTML = number15;
document.getElementById("randomNumber16").innerHTML = number16;
document.getElementById("randomNumber17").innerHTML = number17;
document.getElementById("randomNumber18").innerHTML = number18;
}
</script>
</head>
<body>
<class id="randomNumber"></class>
<class id="randomNumber2"></class>
<class id="randomNumber3"></class>
<class id="randomNumber4"></class>
<class id="randomNumber5"></class>
<class id="randomNumber6"></class>
<class id="randomNumber7"></class>
<class id="randomNumber8"></class>
<class id="randomNumber9"></class>
<class id="randomNumber10"></class>
<class id="randomNumber11"></class>
<class id="randomNumber12"></class>
<class id="randomNumber13"></class>
<class id="randomNumber14"></class>
<class id="randomNumber15"></class>
<class id="randomNumber16"></class>
<class id="randomNumber17"></class>
<class id="randomNumber18"></class>
</body>