首页 > 技术文章 > JS产生随机一注彩票

llz1314 2016-08-16 19:28 原文

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>彩票</title>
    <style type="text/css">
    #wrap{
        width: 300px;height: 150px;
        border: 1px solid red;
        margin: 100px auto 0px;
        text-align: center;
        position: relative;
    }
    #btn{
        width: 100px;height: 50px;
        font-size: 20px;
        margin-top: 10px;
    }
    /*div{
        display: inline-block;
    }*/
    #one div{
        width: 50px;height: 50px;
        border: 1px solid black;
        display: inline-block;
        margin-top: 50px;
        line-height: 50px;
    }

    
    </style>
</head>
<body>
    <div id="wrap">
        <input type="button" name="" id="btn" value="随机一注">
        <div id="one">
            <div ></div>
            <div ></div>
            <div ></div>
            <div ></div>
            <div ></div>
        </div>
    </div>
    <script type="text/javascript">

  //获取元素
    var btn = document.getElementById('btn')
    var odiv = document.getElementById('one')
    var div = one.getElementsByTagName('div')

  //定义点击事件
    btn.onclick = function(){
        for(var i=0;i<div.length;i++){
            var num = Math.floor(Math.random() * 100);
            div[i].innerHTML=num;
            if(div[i].innerHTML < 10){
            div[i].innerHTML = '0'+ div[i].innerHTML;
            }
        }

    }
    </script>
</body>
</html>

推荐阅读