一个简单的随机点名器。

//实现思路:
弄两个函数,一个开始函数,一个停止函数。。
点击开始按钮的时候,开始随机筛选。
点击结束按钮的时候。结束筛选。 清除定时器。
用到的知识点:数组,随机数,setinterval,clearinterval,点击事件函数

<!DOCTYPE html>
<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>

    </head>

    <body>
        <div id="box">
            <p><span id="">黄忠</span></p>
            <button onclick="run_star()" id="begin">点击开始</button>
            <button onclick="run_stop()" id="stop">点击结束</button>
        </div>
        <script type="text/javascript">
            var span = document.getElementById('box').getElementsByTagName('span')[0]; //获取span节点
            var btn_star = document.getElementsByTagName('button')[0]; //获取开始按钮的节点
            var btn_stop = document.getElementsByTagName('button')[1]; //获取结束按钮的节点
            var time; //先定义个空的定时器变量,等待赋值
            function run_star() { //开始按钮 的函数
                time = setInterval(function() {
                    var arr = ['张飞', '关羽', '赵云', '吕布', '刘备', '曹操']; //定义一个容器来存储名字
                    var arr_index = Math.floor(Math.random() * arr.length); //随机生成一个数组的索引值
                    var arr_random = arr[arr_index]; //通过索引值随机生成的数组中的某个名字
                    span.innerHTML = arr_random; //将随机生成的这个名字赋值给span的innerHTML属性。
                }, 100)
            }

            function run_stop() { //结束按钮的函数
                clearInterval(time); //清除定时器,即停止通过time启动的这个函数。
            }
        </script>
        <style type="text/css">
            div {
                position: relative;
                margin: 0 auto;
                width: 300px;
                height: 300px;
                background: burlywood;
            }

            div p {
                font-weight: bold;
                color: darkgreen;
                font-size: 30px;
                width: 100%;
                position: absolute;
                top: 100px;
                text-align: center;
            }

            #begin {
                position: absolute;
                float: left;
                bottom: 0px;
            }

            #stop {
                float: right;

            }
        </style>
    </body>

</html>
好的,以下是一个简单的 web 随机点名的实现,使用了 HTML、CSS、JavaScript 和 PHP: 1. 创建一个名为 `index.html` 的 HTML 文件,用于显示随机点名的界面。可以使用 Bootstrap 和 jQuery 等库来快速构建界面,如下所示: ```html <!DOCTYPE html> <html> <head> <title>随机点名</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <style> body { background-color: #f2f2f2; padding-top: 50px; } .jumbotron { background-color: #e9ecef; text-align: center; } </style> </head> <body> <div class="container"> <div class="jumbotron"> <h1>随机点名</h1> <p>点击下面的按钮进行随机点名</p> <button class="btn btn-primary btn-lg" id="pickBtn">点名</button> </div> <div class="alert alert-success" id="result" style="display: none;"> <strong>被点名的同学是:</strong><span id="name"></span> </div> </div> <script> $(document).ready(function(){ $("#pickBtn").click(function(){ $.get("pick.php", function(data, status){ $("#name").text(data); $("#result").show(); }); }); }); </script> </body> </html> ``` 这个界面使用了 Bootstrap 的样式和组件,包括 jumbotron、btn、alert 等。添加了一个按钮用于触发点名程序,一个用于显示结果的 DIV,以及一个用于显示被选中的名字的 SPAN。使用了 jQuery 的 AJAX 方法来异步加载 PHP 文件中返回的结果。 2. 创建一个名为 `pick.php` 的 PHP 文件,用于处理随机点名的逻辑,如下所示: ```php <?php $names = array('张三', '李四', '王五', '赵六', '陈七'); $index = array_rand($names); echo $names[$index]; ?> ``` 这个 PHP 文件创建了一个包含多个名字的数组,使用 `array_rand()` 函数生成一个随机数作为选中的名字的索引。最后输出被选中的名字。 3. 将 `index.html` 和 `pick.php` 两个文件放在一个 PHP 服务上,比如 Apache 或 Nginx。打开 `index.html` 文件,点击按钮即可进行随机点名
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值