javascript运行时错误保存/恢复

时间:2012-10-15 16:50:33

标签: javascript html css runtime-error

我已经创建了这个代码,当我按下保存并在我按下恢复时返回到该状态时将其存储在变量中,但我似乎在最后一个代码(表的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 个答案:

答案 0 :(得分:1)

我认为#sudoku<table>元素,不是吗? Internet Explorer does not allow setting the innerHTML property on table elements

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

答案 1 :(得分:0)

这是IE的长期问题,并使用innerHTML属性插入新的HTML (哎呀,谁猜到了,IE的问题!!)

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

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

否则,如果您能以某种方式将html放入innerHTML标记的<div>属性中,它应该有用。

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

答案 2 :(得分:0)

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

E.G。

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

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

FOR Reference:https://developer.mozilla.org/en-US/docs/DOM/Node.cloneNode

答案 3 :(得分:-1)

你添加的代码看起来很好,但我认为你在每个陈述后都缺少分号。


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

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


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

例如

    `$('#sudoku').html(clone);`