在网页编程中,经常会用到随机数,像生成订单号这些都会用到。

今天小编分享的是使用javascript生成随机四位数,以下为实例代码。

    <input type="button" id="btn" value="点击生成随机四位数" />
    <span id="num"></span>
    <script>
    function rand(min,max) {
        return Math.floor(Math.random()*(max-min))+min;
    }
    var btn=document.getElementById("btn");
    btn.onclick=function(){
        var randnum=rand(1000,9999);
        document.getElementById("num").innerHTML=randnum;
    }
    </script>

当然你还可以修改rand里面的数字,达到自己想要的效果。

原文转自三体教程http://www.santii.com/article/133.html

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-18
  • 2021-07-26
  • 2021-12-18
  • 2021-06-15
猜你喜欢
  • 2021-09-08
  • 2022-12-23
  • 2021-12-12
  • 2021-12-18
  • 2022-12-23
  • 2021-12-26
  • 2021-12-18
相关资源
相似解决方案