0

数独テーブルを保存および復元するために cloneNode を使用しました。ただし、クローンを作成してからテーブルを復元すると、復元されたバージョンは編集できなくなります。

//Create Save Function and store in a cloneNode
function Save(){
var table = document.getElementById("sudoku");
clone = table.cloneNode(true);  
}
//Create Restore Function and restore saved state from the cloneNode and delete parent table
function Restore(){
var table = document.getElementById("sudoku"),
parent = table.parentNode;
parent.removeChild(table);
parent.appendChild(clone);
}

ここに私のイベントハンドラがあります

function selectCell() {
if (current_cell !== null) {
    current_cell.className = 'tofill';
}
current_cell = this;
current_cell.className = 'selected';
 }

// Capture keyboard key presses. If the key pressed is a digit
// then add it to the current cell. If it is a space then empty
// the current cell.
function keyPress(evt) {
if (current_cell == null)
    return;
var key;
if (evt)
    // firefox or chrome
    key = String.fromCharCode(evt.charCode);
else
    // IE
    key = String.fromCharCode(event.keyCode);
if (key == ' ')
    current_cell.innerHTML = '';
else if (key >= '1' && key <= '9')
    current_cell.innerHTML = key;
  }

イベント リスナーを再アタッチして、保存後にテーブルを復元したときにテーブルを編集できるようにするにはどうすればよいですか。

編集

 var current_cell = null; // the currently selected cell
 var saved = {};        // Object for saving the current game
 function initialize() {
var col, row;
// Work through all the cells in the table and set
// onclick event handlers and classNames for the empty
// ones.
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;
save();
    }

これは私がイベントハンドラーをアタッチする方法ですが、同じ方法で再アタッチすると、まだ同じ問題があります

4

1 に答える 1

0
var current_cell = null; // the currently selected cell
var saved = {};        // Object for saving the current game
function initialize() {
var col, row;
// Work through all the cells in the table and set
// onclick event handlers and classNames for the empty
// ones.
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;
save();
}

このコードをresotre関数に再挿入して、イベントリスナーを再接続しました

于 2012-10-25T11:16:04.320 に答える