1

我已经创建了这个代码,它用于在我按下保存时将表存储在一个变量中,并在我按下恢复时返回到该状态,但我似乎在最后一个代码上遇到了运行时错误(表的 ID 是数独)它适用于Firefox,但不适用于IE,谢谢

var clone
function save()
{
    var table = document.getElementById("sudoku")
    clone = table.innerHTML
}

function restore()
{
    document.getElementById("sudoku").innerHTML=clone
}

编辑:错误消息:

Webpage error details User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; CPNTDF; .NET4.0E; .NET4.0C; BOIE9;ENUS) Timestamp: Mon, 15 Oct 2012 16:57:44 UTC Message: Unknown runtime error Line: 50 Char: 128 Code: 0 URI: file:///F:/uni%20work/home/Second%20year/CO525/assessments/Assessment2/assessmen‌​t2/javascript.js Message: Unknown runtime error Line: 50 Char: 128 Code: 0 URI: file:///F:/uni%20work/home/Second%20year/CO525/assessments/Assessment2/assessmen‌​t2/javascript.js

编辑 完整代码:

    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();
    }

    // mouse button event handler
    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 clone
    function save()
    {
    var table = document.getElementById("sudoku");
    clone = table.innerHTML;
    }

   function restore()
    {
    document.getElementById("sudoku").innerHTML=clone;
   }
4

4 回答 4

1

我假设#sudoku是一个<table>元素,不是吗?Internet Explorer 不允许innerHTML在表格元素上设置属性

相反,请使用适当的 DOM 方法,或者不要尝试存储 HTML 字符串,而是将数独的内容存储在二维数组中。

于 2012-10-15T17:14:08.780 回答
0

innerHTML这是 IE 和使用该属性插入新 HTML 的长期问题。
(哎呀,谁会猜到,IE 有问题!!)

如果您愿意使用 jQuery,那么您可以使用...

$("#mytable").html(myHtml);

否则,如果您可以以某种方式将 html 放入标签的innerHTML属性中,它应该可以工作。<div>

document.createElement("TR");另一种选择是使用编码风格自己创建单个对象。

于 2012-10-15T17:16:07.930 回答
0

您可以使用 DOM 内置的 javascript 方法cloneNode来克隆您的节点。

例如

 var clone;
    function save()
    {
        var table = document.getElementById("sudoku");
        clone = table.cloneNode(true);
    }

    function restore()
    {
        document.getElementById("sudoku").parentNode.appendChild(clone);
    }

参考:https ://developer.mozilla.org/en-US/docs/DOM/Node.cloneNode

于 2012-10-15T17:23:21.887 回答
-1

您添加的代码看起来不错,但我认为您在每个语句后都缺少分号。

    变种克隆;
    函数保存()
    {
        var table = document.getElementById("sudoku");
        克隆 = table.innerHTML;
    }

    函数恢复()
    {
        document.getElementById("sudoku").innerHTML=clone;
    }


你也可以通过 jQuery html 方法。如果您想了解更多关于 html() 函数的信息,请单击此处jQuery html () 函数。

例如

    `$('#sudoku').html(克隆);`
于 2012-10-15T17:16:50.607 回答