使用JavaScript,我想在soduku.setstring()中填充数字;用户单击“Easy”按钮后进入表格。如何使用onClick()函数实现此目的?
function initialize() {
var col, row;
for (row = 0; row <=8; row++) {
for (col=0; col <= 8; col++) {
var cell = document.getElementById('cell_' + col + '_' + row);
if (!parseInt(cell.innerHTML)) {
// cell is empty
cell.onclick = selectCell;
cell.className = 'tofill';
}
}
}
document.onkeypress = keyPress;
}
function easygame(){
var sudoku.setString("240057360000008009378000200060041702080000005000005010130000000050003026000812000");
loadPuzzle(sudoku);
updateUI();
}
function updateUI(){
initialize();
}
<input type="button" value="Easy" onclick="easygame()"/>
<table align=center id="sudoku" cellspacing=0>
<tr>
<td id="cell_0_0"></td>
<td id="cell_1_0"></td>
<td id="cell_2_0"></td>
<td id="cell_3_0"></td>
<td id="cell_4_0"></td>
<td id="cell_5_0"></td>
<td id="cell_6_0"></td>
<td id="cell_7_0"></td>
<td id="cell_8_0"></td>
<tr>
答案 0 :(得分:-1)
对于数独游戏,为什么不使用2D数组。它在逻辑上更容易,而不是使用字符串。就你的问题而言,你可以使用jQuery来填写这样的表,
for(var i=0;i<81;i++)
{
$('table#sudoku td:eq('+i+')').text(sudokuString[i]);
}