<!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>