javascript运行时错误保存/还原
我创建了这段代码,当我按save时,它将一个表存储在一个变量中,当我按restore时,它将返回到该状态。但是我似乎在最后一段代码(表的Id是sudoku)中遇到了运行时错误。它在firefox中工作,但在IE中不工作,谢谢javascript运行时错误保存/还原,javascript,html,css,runtime-error,Javascript,Html,Css,Runtime Error,我创建了这段代码,当我按save时,它将一个表存储在一个变量中,当我按restore时,它将返回到该状态。但是我似乎在最后一段代码(表的Id是sudoku)中遇到了运行时错误。它在firefox中工作,但在IE中不工作,谢谢 var clone function save() { var table = document.getElementById("sudoku") clone = table.innerHTML } function restore() { doc
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;
}
var current_cell=null;//当前选定的单元格
var saved={};//对象以保存当前游戏
函数初始化(){
var col,row;
//完成表格中的所有单元格并设置
//onclick事件处理程序和空
//一个。
对于(row=0;row我假设#数独
是一个
元素,不是吗
相反,请使用适当的DOM方法,或者不要尝试存储HTML字符串,而是将数独的内容存储在二维数组中。这是IE和使用innerHTML
属性插入新HTML的长期问题。
(天哪,谁会想到,IE有问题!!)
如果您愿意使用jQuery,那么可以使用
$("#mytable").html(myHtml);
否则,如果您能够以某种方式将html放入
标记的innerHTML
属性中,它应该可以工作
另一种选择是使用document.createElement(“TR”);
编码风格自己创建单个对象。您添加的代码看起来不错,但我认为您在每个语句后都缺少分号
var clone;
function save()
{
var table = document.getElementById("sudoku");
clone = table.innerHTML;
}
function restore()
{
document.getElementById("sudoku").innerHTML=clone;
}
变种克隆;
函数save()
{
var table=document.getElementById(“数独”);
clone=table.innerHTML;
}
函数还原()
{
document.getElementById(“数独”).innerHTML=clone;
}
如果您想了解更多关于html()函数的信息,请单击此处
e、 g
`$('#数独').html(克隆)`
您可以使用DOM内置javascript方法克隆节点
例如
参考:发布完整代码..和错误消息..请给出实际错误消息抱歉,第一次使用此功能时,整个代码太多,无法发布。是否有其他方法?网页错误详细信息用户代理:Mozilla/4.0(兼容;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)时间戳:Mon,2012年10月15日16:57:44 UTC消息:未知运行时错误行:50字符:128代码:0 URI:file:///F:/uni%20work/home/Second%20year/CO525/assessments/Assessment2/assessment2/javascript.js 消息:未知运行时错误行:50字符:128代码:0 URI:file:///F:/uni%20work/home/Second%20year/CO525/assessments/Assessment2/assessment2/javascript.jsFirst guess:clone
的内容被破坏了。是的,这是一个表的ID。我明白了,还有其他方法吗?链接的问题有一些答案;除此之外,我只能建议不要使用innerHTML
,我很怀疑,我只是不想麻烦地创建一个表arrayJavascript不需要在每条语句的末尾加分号(与C/C++/C#/etc不同),因此这对当前的问题没有任何影响@Jimmy,只是给了你另一个选择,如果你有这个选择的话,我只需要用$(“#mytable”).html(myHtml)替换整个代码?不@Jimmy,不是整个代码…该命令纯粹用于设置所选对象的内部html,因此在您的情况下,它将用$(“#sudoku”).html(克隆)替换文档.getElementById(“sudoku”).innerHTML=clone
行
恐怕这会创建一个副本,而不是删除第一个,并用以前的@Ashirvadit替换它,从而创建节点的精确副本。为了便于解释,请访问“是”,但副本是在旧表下创建的,因此浏览器上有两个表,而不是一个。然后,您可以像这样清除第一个表添加此l还原函数document.getElementById(“数独”).parentNode.removeChild(document.getElementById(“数独”))中的行;
`$('#sudoku').html(clone);`
var clone;
function save()
{
var table = document.getElementById("sudoku");
clone = table.cloneNode(true);
}
function restore()
{
document.getElementById("sudoku").parentNode.appendChild(clone);
}