gpt4 book ai didi

javascript 运行时错误保存/恢复

转载 作者:太空宇宙 更新时间:2023-11-03 18:58:03 25 4
gpt4 key购买 nike

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

最佳答案

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

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

关于javascript 运行时错误保存/恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12900286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com