简体   繁体   English

JavaScript在IE中不起作用:Sudoku Puzzle

[英]JavaScript doesn't work in IE: Sudoku Puzzle

http://home.earthlink.net/~benfranq/Sudoku.html http://home.earthlink.net/~benfranq/Sudoku.html

Perhaps somebody can tell me why it doesn't work in Internet Explorer 也许有人可以告诉我为什么它在Internet Explorer中不起作用

In other browsers, it seems to work fine. 在其他浏览器中,它似乎可以正常工作。

I've tried adding semicolons to every line, but it still doesn't work. 我尝试过在每行中添加分号,但仍然无法正常工作。

This isn't my page, a friend asked me to figure out why it isn't working in IE but ok in other browsers. 这不是我的页面,一个朋友问我为什么无法在IE中使用它,但在其他浏览器中也可以。 I'm moving it to my server since he won't allow me ftp to his server. 我将其移动到服务器上,因为他不允许我ftp到他的服务器上。 Anyways, I'll post the new link with my semicolon changes right shortly. 无论如何,我很快就会发布带有分号更改的新链接。

Semicolons 分号

I don't think it's the only reason, but you should probably be ending each statement with a semicolon. 我认为这不是唯一的原因,但是您可能应该在每个语句后使用分号结束。 JavaScript will let you get away without doing this sometimes, but it's always a good practice just to do it anyway. JavaScript有时会让您无所事事,但是无论如何总是这样做是一个好习惯。

Global/Window Scope 全局/窗口范围

function hints(){
   for(c=0;c<N4;c++)if(status[c]==" "){  // unsolved cell 
      if(ruleCell(c)>0){ high(c) ; continue }                       
   }
}   

Do you want to be manipulating c from within hints? 您是否要从提示中操作c Here, you'll be manipulating it in the global scope. 在这里,您将在全局范围内进行操作。

setAttribute('onclick') setAttribute('onclick')

IE misbehaves when doing elem.setAttribute("onclick",...) , instead try using the event handling model. IE在执行elem.setAttribute("onclick",...)时行为异常,而是尝试使用事件处理模型。 Here's a section on Wikipedia about Microsoft-Specific DOM Event handling. 这是Wikipedia上有关Microsoft特定DOM事件处理的部分。

首先猜测:您必须以分号结尾

There are three problems. 有三个问题。

  1. The elem.setAttribute("onclick",...) issue that JasonWyatt identified, JasonWyatt识别出的elem.setAttribute("onclick",...)问题,

  2. The javascript uses square bracket notation to index the characters in a string. javascript使用方括号表示法来索引字符串中的字符。 Use .charAt() instead. 使用.charAt()代替。

  3. The elem.setAttribute("class",...) should be elem.className = ... elem.setAttribute("class",...)应该是elem.className = ...

After running this through IE8's debugger (Press F12, it's actually a nice tool), I found the problem: IE8 is not treating your strings as an array of characters. 通过IE8的调试器(按F12键,它实际上是一个不错的工具)运行此程序后,我发现了问题:IE8并未将字符串视为字符数组。

You're storing puzzle (and solution, among other things) as a string. 您正在将难题(以及解决方案等)存储为字符串。 In chrome/firefox, solution[2] will return the 3rd character in that string. 在chrome / firefox中,solution [2]将返回该字符串中的第三个字符。 However, in IE8, it returns undefined. 但是,在IE8中,它返回undefined。 The workaround is to use solution.charAt(2); 解决方法是使用solution.charAt(2);。

I also took the liberty of removing setAttribute calls and instead I'm setting the properties directly. 我还自由删除了setAttribute调用,而是直接设置属性。

You can find the fixed code here (which works in IE8): http://pastebin.org/51648 您可以在此处找到固定的代码(在IE8中有效): http : //pastebin.org/51648

I think its because the first line is commented. 我认为是因为第一行被注释。 When I see view source, the javascript line breaks are gone and therefore the entire javascript code is interpreted as a comment. 当我看到查看源代码时,javascript换行符消失了,因此整个javascript代码都被解释为注释。

My suggestion: try removing the commented code or try block comments ( /* */ ) 我的建议:尝试删除注释的代码或尝试阻止注释( /* */

I copy pasted the source to my local machine, converted the // to /**/ and it seems to work (i admit, i just saw that things were better. I did not test ). 我将源代码复制粘贴到我的本地计算机上,将//转换为/**/ ,并且似乎可以正常工作(我承认,我只是发现情况更好。我没有进行测试 )。

Generally: If the javascript is compressed, by removing unnecessary spaces and new lines, avoid // comments as much as possible 通常:如果javascript被压缩,则通过删除不必要的空格和换行符,请尽可能避免//注释

(but if some program (the webserver?) is compressing javascript by removing unnecessary whitespaces shouldn't it also remove the comments?) (但是,如果某个程序(网络服务器?)通过删除不必要的空格来压缩 javascript,是否也应该删除注释?)

EDIT: To clarify: When I did view source in IE, I saw the javascript as a single looong line. 编辑:澄清一下:当我在IE中查看源代码时,我将javascript视为单个looong行。 In IE8's in-built source viewer (how do you call that?) and also when I copy pasted to Notepad 在IE8的内置源代码查看器中 (您怎么称呼它?),以及当我将粘贴复制到记事本中时

When I did view-source in Safari (Mac), the javascript was properly in different lines. 当我在Safari(Mac)中执行视图源时,JavaScript正确地位于不同的行中。

So: 所以:

  1. no javascript compressing going on here as I mentioned before. 正如我之前提到的,此处没有进行javascript压缩。

  2. I suspect line endings. 我怀疑行尾。 Do you develop on a non Windows machine, or is the editor on your windows machine configured to have a different line ending? 您是在非Windows计算机上进行开发,还是Windows计算机上的编辑器配置为具有不同的行尾? (CR in place of CR/LF) (用CR代替CR / LF)

(anyway first try removing commented code to be sure. And then think about line endings) (确保首先尝试删除注释的代码。然后考虑行尾)

try to use different variables names. 尝试使用不同的变量名称。 for some reason IE has restrictions about using some variables names like class or msie. 由于某些原因,IE限制了使用某些变量名称(如class或msie)的限制。 I made a list and will share it soon :) 我列出了清单,很快就会分享:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM