保存を押したときにテーブルを変数に保存し、復元を押したときにその状態に戻るようにこのコードを作成しましたが、最後のコードでランタイム エラーが発生したようです (テーブルの 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/assessment2/javascript.js Message: Unknown runtime error Line: 50 Char: 128 Code: 0 URI: file:///F:/uni%20work/home/Second%20year/CO525/assessments/Assessment2/assessment2/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;
}