javascript - 如何在每行和每列中生成随机数

标签 javascript html

我想在每一行和每一列中生成随机数。我这样写在下面。它只生成两行。我想创建 4*4 数独。这样我就这样给了

    <script type="text/javascript">
  var random1, random2, random3, random4;
var randomArray = [0,0,0,0];


//sets array variables to random numbers
function CreateLottoValues() {
for (var i=0; i<randomArray.length; i++) {

randomArray[i] = Math.floor(Math.random()*4 + 1);


}
}
}

//create table
function UpdateTable() {
CreateLottoValues();
for (var i=0; i<randomArray.length; i++) {
tmp = 'cell'+(i+1);
document.getElementById(tmp).innerHTML = randomArray[i];     
}

}
</script>
</head>
<body onload="UpdateTable();">
<center>
<div id="container">

<div id="header"> <h1>Welcome</h1> </div>

<div id="content">
<span id="tableSpan">
<table border="1" id="lotto">
<tr>
<td class="normal" id="cell1">&nbsp;</td>
<td class="normal" ><input type="text" name=""></td>
<td class="normal" >&nbsp;</td>
<td class="red" id="cell4">&nbsp;</td>
</tr>
<tr>
<td class="normal" id="cell1">&nbsp;</td>
<td class="normal" id="cell2">&nbsp;</td>
<td class="normal" id="cell3">&nbsp;</td>
<td class="red" id="cell4">&nbsp;</td>
</tr>
<tr>
<td class="normal" id="cell1">&nbsp;</td>
<td class="normal" id="cell2">&nbsp;</td>
<td class="normal" id="cell3">&nbsp;</td>
<td class="red" id="cell4">&nbsp;</td>
</tr>
<tr>
<td class="normal" id="cell1">&nbsp;</td>
<td class="normal" id="cell2">&nbsp;</td>
<td class="normal" id="cell3">&nbsp;</td>
<td class="red" id="cell4">&nbsp;</td>
</tr>
</table>
</span>
<input type="button" value="Re-generate Numbers" onclick="UpdateTable();" />

请帮我在所有行和列中生成随机数。提前致谢。

最佳答案

确保您的 HTML 中有唯一的 Id

同时避免使用内联事件。使用 javascript 绑定(bind)事件,这更加简洁并且可以分离您的顾虑。

HTML

<center>
    <div id="container">
        <div id="header">
             <h1>Welcome</h1> 
        </div>
        <div id="content">
            <table border="1" id="lotto">
                <tbody>
                    <tr>
                        <td class="normal" id="cell00">&nbsp;</td>
                        <td class="normal" id="cell01">&nbsp;</td>
                        <td class="normal" id="cell02">&nbsp;</td>
                        <td class="red" id="cell03">&nbsp;</td>
                    </tr>
                    <tr>
                        <td class="normal" id="cell10">&nbsp;</td>
                        <td class="normal" id="cell11">&nbsp;</td>
                        <td class="normal" id="cell12">&nbsp;</td>
                        <td class="red" id="cell13">&nbsp;</td>
                    </tr>
                    <tr>
                        <td class="normal" id="cell20">&nbsp;</td>
                        <td class="normal" id="cell21">&nbsp;</td>
                        <td class="normal" id="cell22">&nbsp;</td>
                        <td class="red" id="cell23">&nbsp;</td>
                    </tr>
                    <tr>
                        <td class="normal" id="cell30">&nbsp;</td>
                        <td class="normal" id="cell31">&nbsp;</td>
                        <td class="normal" id="cell32">&nbsp;</td>
                        <td class="red" id="cell33">&nbsp;</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    <input id="btn" type="button" value="Re-generate Numbers" />
</center>

JS

var btn = document.getElementById('btn');

btn.addEventListener('click', UpdateTable);


// Set the max length of random number 
// and the max length
var maxLength = 4;
// You don't require an empty array here
// to populate the list of random numbers.

// Returns a random number
function CreateLottoValues() {
    return Math.floor(Math.random() * 4 + 1);
}

//create table
function UpdateTable() {
    // Iterate over each cell and set a random number
    for (var i = 0; i < maxLength; i++) {
        for (var j = 0; j < maxLength; j++) {
            tmp = 'cell' + i + j;
            document.getElementById(tmp).innerHTML = CreateLottoValues();
        }
    }
}

UpdateTable();

Check Demo

关于javascript - 如何在每行和每列中生成随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18159394/

相关文章:

html - 如何隐藏特定标题下的表格列?

javascript - 如何将输入标签值传递给(点击)函数并在服务 Angular 8 中使用

javascript - 设置包含保留字的代码CSS

javascript - 我可以使用 jQuery 访问不存在或无效的 css 属性吗?

javascript - 浏览器控制台输出在带有 settimeout 的 for 循环中有一个神秘的数字?

javascript - 显示 onclick 函数后从数据库检索的推文

javascript - 重写Ext.data.Store,调用构造函数

java - 每隔 1 秒将值按顺序写入 ArrayList 中的 JSP 页面?

css - 缩略图网格图像调整大小问题

javascript - 在 bigcommerce 中打开 Popup 一次