5

我正在开发一个 Javascript 项目并使用 Chrome F12 开发人员工具进行调试。出于某种原因,所有 console.log 输出和错误消息都声称它们发生在我的 js 文件的第 1 行。

...即,在每行右侧的控制台中,myFile.js:1即使第 1 行没有代码,它也会显示,并且代码显然在不同的行上运行。

这可能是什么原因?

4

3 回答 3

2

Apparently other people weren't able to reproduce the problem (@Bergi), so I can only imagine that Chrome was somehow having a problem with the file (corrupted?). Clearing cache did not help.

The work-around solution that worked for me was to use a new file.

  1. Change file name to myFile_broken.js
  2. Create a new file myFile.js
  3. Copy all content from myFile_broken.js to myFile.js

The new js file now displays the correct line numbers. Despite all properties looking the same and all text content being the same, the new file had about 100 more bytes than the original broken file.

Hopefully this helps someone who has the same issue, and hopefully the root problem is one day discovered and fixed. (Error reproduced by me in Chrome versions 34.0.1847.116 m and 34.0.1847.131 m)

于 2014-04-26T18:17:34.327 回答
1

这听起来像是行尾格式不正确的问题。这可能是您的编辑器使用的设置有问题,甚至可能是文件复制到服务器的方式有问题。但无论出于何种原因,这些行都没有被识别为具有正确编码的结尾,因此它们都被视为一行。

于 2014-04-26T18:34:42.063 回答
1

我最近遇到了一个 JS 错误,它在控制台中显示为第 1 行。事实证明,错误来自动态构造的onchange属性内部。

由于错误出现在属性内代码的第一“行” onchange,并且没有与内联属性关联的文件名,因此 Firefox 控制台感到困惑,并将错误显示为来自周围 HTML 文件的第 1 行。Chrome 控制台也将其显示为第 1 行,但单击错误会打开 onchange 处理程序,而不是打开整个 html 文件,这就是我想出来的。

于 2018-03-16T18:15:42.337 回答