javascript 未捕获的语法错误:Chrome 浏览器的意外令牌非法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7764035/
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-26 01:14:11  来源:igfitidea点击:

Uncaught SyntaxError: Unexpected token ILLEGAL for Chrome Browser

javascriptjquerygoogle-chrome

提问by TiSer

toggle.js

切换.js

var $jq = jQuery.noConflict();
$jq(document).ready(function(){

    $jq('.isAdd').hide();

    $jq("#Add_category").change(function(){          
        var value = $jq("#Add_category option:checked").val();
        var theDiv = $jq(".isAdd");

        theDiv.slideToggle("slow");
    });
});?

In console I had:

在控制台我有:

Uncaught SyntaxError: Unexpected token ILLEGAL

未捕获的语法错误:意外的令牌非法

For Firefox it's works fine, but not for Chrome and Chromium for Ubuntu.

对于 Firefox,它工作正常,但不适用于 Chrome 和 Chromium for Ubuntu。

回答by Michael Berkowski

There is an invisible character following the last });of your last line. When I pasted it into my editor, it appeared as a ..

});最后一行的最后一个后面有一个不可见的字符。当我将它粘贴到我的编辑器中时,它显示为..

View your code in an editor capable of displaying non-printable characters with some kind of symbol, or view it in a hex editor.

在能够显示带有某种符号的不可打印字符的编辑器中查看您的代码,或在十六进制编辑器中查看它。

回答by Syed Priom

To summarize the solution to problem with "Unexpected Token ILLEGAL":

总结“Unexpected Token ILLEGAL”问题的解决方案:

ILLEGAL means strictly Syntax Error.

非法意味着严格的语法错误。

The Fix

修复

Install HxD Editor (recommended) and open your file in it. You can detect where exactly the error is occuring by detecting unusual .(as it happened with me), with the code's HEX representation. Save and replace the file.

安装 HxD Editor(推荐)并在其中打开您的文件。您可以通过.使用代码的十六进制表示检测异常(就像我发生的那样)来检测错误发生的确切位置。保存并替换文件。

回答by Irishka

@TiSer: please check your code for invisible white space chars, on document ready handler is not the problem here, it was the illegal token direct after them – Irishka Oct 14 '11 at 13:57

yeap, it's right! Incredible - one small symbol and all JS code fell. :) – TiSer Oct 17 '11 at 8:50

@TiSer:请检查您的代码是否有不可见的空白字符,文档就绪处理程序在这里不是问题,而是直接在它们之后的非法令牌 – Irishka 2011 年 10 月 14 日 13:57

是的,没错!难以置信 - 一个小符号,所有 JS 代码都掉下来了。:) – TiSer 2011 年 10 月 17 日 8:50

$jq(document).ready(...); is causing this error Chrome

$jq(document).ready(...); 导致此错误 Chrome

move your jQuery script to the bottom before < /body > tag

将你的 jQuery 脚本移到 </body> 标签之前的底部

EDIT: check for white spaces

编辑:检查空格