zoukankan      html  css  js  c++  java
  • 生成随机数,且每个数一定不相同

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
        <title>无标题文档</title>
        <style type="text/css">
            .textbox
            {
                margin-right: 5px;
                margin-top: 10px;
                 25px;
                font-family: 微软雅黑;
                text-align: center;
                font-weight: bold;
                font-size: 16px;
                color: Blue;
            }
        </style>
        <script type="text/javascript">
            function CreateTextBox() {
                var count = GetTextBoxCount();
                if (count < 10) {
                    var textBox = document.createElement("input");
                    textBox.setAttribute("type", "text");
                    textBox.setAttribute("name", "myname");
                    textBox.className = "textbox";
                    document.body.appendChild(textBox);
                } else {
                    alert("最多生成10个随机数");
                }
            }
            function RemoveTextBox() {
                var count = GetTextBoxCount();
                if (count > 1) {
                    document.body.removeChild(document.body.lastChild);
                }
                else {
                    alert("请最少保留1个");
                }
            }
            function GetTextBoxCount() {
                var elements = document.getElementsByName("myname");
                return elements.length;
            }
            Array.prototype.Contains = function (num) {
                var flag = false;
                if (this.length <= 0) {
                    return flag;
                }
                for (var i = 0; i < this.length; i++) {
                    if (this[i] == num) {
                        flag = true;
                        break;
                    }
                }
                return flag;
            }
            function CreateRandomNumber(array, count) {
                while (array.length < count) {
                    var num = Math.floor(Math.random() * 10);
                    if (!array.Contains(num)) {
                        array.push(num);
                    }
                }
            }
            function SetTextBoxValue() {
                var array = new Array();
                var count = GetTextBoxCount();
                CreateRandomNumber(array, count);
                var elements = document.getElementsByName("myname");
                for (var i = 0; i < elements.length; i++) {
                    elements[i].value = array[i];
                }
            }
        </script>
    </head>
    <body onload="CreateTextBox()">
        <input type="button" value="增加" onclick="CreateTextBox()" />
        <input type="button" value="减少" onclick="RemoveTextBox()" />
        <input type="button" value="生成随机数" onclick="SetTextBoxValue()" /><br />
    </body>
    </html>

  • 相关阅读:
    JavaEE 7学习笔记
    RX232串口发送
    以8位并行数据为例确定crc-32的一般矩阵表示形式
    nios ii 13 主程序的函数可以用Open Declaration 查看,但是编译的时候却说 undefined reference to 。。。这是为什么?
    做uart 实验时,run configure 只能选择jtag_uart 而没有uart
    在做nios ii uart232 实验时出现undefined reference to `fclose'等错误。
    修改quartus 配置rom时memory很小的问题。
    关于VGA显示实验的问题
    Microsoft Visual Studio 2013 已停止工作的解决方法
    独家原创,拖拽任意控件移动任意目标,拖拽控件移动整个窗体
  • 原文地址:https://www.cnblogs.com/snowbaby-kang/p/3937281.html
Copyright © 2011-2022 走看看