javascript克隆节点重新附加事件侦听器

javascript克隆节点重新附加事件侦听器,javascript,clone,Javascript,Clone,我使用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

我使用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;
  }
函数selectCell(){
if(当前_单元格!==null){
当前_cell.className='tofill';
}
当前_单元=此;
当前_cell.className='选定';
}
//捕捉键盘按键。如果按下的键是数字
//然后将其添加到当前单元格中。如果它是一个空间,那么它是空的
//当前单元格。
功能按键(evt){
if(当前_单元格==null)
返回;
var键;
如果(evt)
//firefox还是chrome
key=String.fromCharCode(evt.charCode);
其他的
//即
key=String.fromCharCode(event.keyCode);
如果(键=='')
当前_cell.innerHTML='';
else if(key>='1'&&key
var current\u cell=null;//当前选定的单元格
var saved={};//用于保存当前游戏的对象
函数初始化(){
var col,row;
//完成表格中的所有单元格并设置
//onclick事件处理程序和空
//一个。

对于(row=0;row)请尝试以下操作:这取决于您是如何附加事件的。为什么不像最初那样重新附加事件?不知道为什么我没有想到。谢谢
 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();
    }
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();
}