javascript IE中的“'null'为空或不是对象”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3744513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:06:24  来源:igfitidea点击:

"'null' is null or not an object" error in IE

javascriptjqueryinternet-explorer

提问by Albinoswordfish

Right now I'm getting this strange error from IE which is basically

现在我从 IE 收到这个奇怪的错误,这基本上是

'null' is null or not an object

The line number it gives is completely off so I don't put much credit into that however I think I narrowed it down to where it's coming from.

它给出的行号是完全关闭的,所以我并没有对此给予太多信任,但是我认为我将其范围缩小到它的来源。

function openSong(songlink){
     $('#smallbox').slideDown();
     requestCompleteSong('<tr><td>'+songlink+'</td></tr>');
  }

The code above doesn't produce any errors from IE, however when I do an AJAX GET request using jQuery then it seems to throw this error

上面的代码不会从 IE 中产生任何错误,但是当我使用 jQuery 执行 AJAX GET 请求时,它似乎抛出了这个错误

function openSong(songlink){
     $('#smallbox').slideDown();
     $.get("getSong.php?fake="+makeid(),{ id: songlink },requestCompleteSong);
  }

'songlink' definitely isn't null because the first function I posted works fine. Here is makeid().

'songlink' 绝对不为空,因为我发布的第一个函数工作正常。这是 makeid()。

function makeid()
{
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for( var i=0; i < 5; i++ )
    text += possible.charAt(Math.floor(Math.random() * possible.length));

  return text;
}

Can anybody see why IE is throwing this error?

有人能明白为什么 IE 会抛出这个错误吗?

采纳答案by Atanas Korchev

This is indeed one of the most generic Internet Explorer JavaScript errors. It means that you are invoking methods of some object which is null. While I cannot tell which part of your code is causing this error I can suggest a way to find it out. You can use the developer tools to debug your code - just put a debuggerstatement in your code:

这确实是最常见的 Internet Explorer JavaScript 错误之一。这意味着您正在调用某个空对象的方法。虽然我无法确定您的代码的哪一部分导致了此错误,但我可以建议一种方法来找出它。您可以使用开发人员工具来调试您的代码 - 只需debugger在您的代码中添加一条语句:

function openSong(songlink){
 debugger;
 // rest of the code
}

Then enable debugging from the IE developer toolbar and refresh your page.

然后从 IE 开发人员工具栏启用调试并刷新页面。