js 发红包

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>无标题文档</title>
        <script>
            window.onload = function (){

                var oBtn = $(‘btn‘);
                var num = $(‘num‘);
                var sum = $(‘sum‘);
                var olist = $(‘list‘);
                var min = 0.01;

                oBtn.onclick = function (){
                    if( num.value && sum.value )
                    {
                        var oSum = sum.value;
                        var oNum = num.value;
                        olist.innerHTML = "";
                        

                        for(i = 1; i <= oNum;i++){

                            if( i == oNum )
                            {

                                olist.innerHTML += "<li>第"+ i +"个红包,您得到 " + oSum +  "元,还剩0元!</li>"
                            }
                            else{
                                var safe_total = (oSum - (oNum - i)*min)/(oNum - i);

                                var money = (getRandom(+min,+safe_total)).toFixed(2);
                                oSum = (oSum - money).toFixed(2);
                                olist.innerHTML += "<li>第"+ i +"个红包,您得到" + money +"元,还剩" + oSum +  "元</li>";

                            }
                        }
    
                    }
                }
                function $(id) {
                return document.getElementById(id);
                }

                function getRandom(two,one){

                return Math.random()*(two-one)+one;
                }
            }


        </script>
        <style>
        </style>
    </head>
    <body>
        
        金额:<input id = "sum" type = "text">
        数量:<input id = "num" type = "text">
        <input id= "btn" type = "button" value= "发红包啦">
        <ul id = "list"></ul>
    </body>
</html>    

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。