代码1:
<script> function getRandom(){ var i = Math.random()*40+160; document.getElementById("num").innerText = window.parseInt(i); } </script> <body onload="getRandom()"> <p id="num"></p> </body>
代码2:
<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=gb2312"> <style> .number{ background:#FF6666; width:30px; height:30px; line-height:30px; border-radius:10px; color: #fff; text-align: center; } </style> <script type="text/javascript"> function random(a, b) { var r = Math.round(Math.random() * (b - a)); document.getElementById("number").innerHTML = a + r; } </script> <title></title> </head> <body onload="random(50,200)"> <div id="number" class="number"></div> </body> </html>