首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >javascript运行时错误保存/恢复

javascript运行时错误保存/恢复
EN

Stack Overflow用户
提问于 2012-10-15 16:50:32
回答 3查看 546关注 0票数 1

我已经创建了这段代码,它的make用于在按restore时将表存储在变量中,然后返回到该状态,但在最后一段代码(该表的Id为sudoku)上似乎遇到了一个运行时错误,它在firefox中工作,但在IE中不工作,谢谢

代码语言:javascript
复制
var clone
function save()
{
    var table = document.getElementById("sudoku")
    clone = table.innerHTML
}

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

编辑:错误消息:

代码语言:javascript
复制
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

编辑完整代码:

代码语言:javascript
复制
    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;
   }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-10-15 17:23:21

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

例如:

代码语言:javascript
复制
 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

票数 0
EN

Stack Overflow用户

发布于 2012-10-15 17:14:08

我猜想#sudoku是一个<table>元素,不是吗?表元素上的

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

票数 1
EN

Stack Overflow用户

发布于 2012-10-15 17:16:07

这是IE和使用innerHTML属性插入新HTML的一个长期问题。

(哎呀,谁会猜到,IE出了问题!!)

如果你愿意去jQuery,那么你可以用.

代码语言:javascript
复制
$("#mytable").html(myHtml);

否则,如果您可以将html放置到innerHTML标记的<div>属性中,它就会工作。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12900286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档