Javascript 未捕获的语法错误:无效或意外的令牌

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

Uncaught SyntaxError: Invalid or unexpected token

javascriptjqueryrazor

提问by Himaan Singh

I have a razor syntax like this:

我有一个像这样的剃刀语法:

   foreach(var item in model)
 {
<td><a href ="#"  onclick="Getinfo(@item.email);" >6/16/2016 2:02:29 AM</a>  </td>
 }

My javascript that recieves the request goes like this:

我收到请求的 javascript 是这样的:

<script type="text/javascript" src="~/Scripts/jquery-1.9.1.js"></script>
<script type="text/javascript">
    function Getinfo(elem) {
        var email = document.getElementById(elem).innerHTML;
    }
</script>

When clicking on the href link, I get the following error in the console of the browser:

单击 href 链接时,我在浏览器的控制台中收到以下错误:

"Uncaught SyntaxError: Invalid or unexpected token",

"未捕获的语法错误:无效或意外的令牌",

and this part is underlined:

这部分有下划线:

    **</a>  </td>**

I am a beginner so I get stuck in syntax a lot. If it is that then please help me out.

我是一个初学者,所以我经常被语法困住。如果是这样,那么请帮助我。

回答by Satpal

You should pass @item.emailin quotes then it will be treated as string argument

您应该传入@item.email引号,然后它将被视为字符串参数

<td><a href ="#"  onclick="Getinfo('@item.email');" >6/16/2016 2:02:29 AM</a>  </td>

Otherwise, it is treated as variable thus error is generated.

否则,它被视为变量,从而产生错误。

回答by Iman Bahrampour

The accepted answer work when you have a single line string(the email) but if you have a

当您有一个单行字符串(电子邮件)但如果您有一个

multiline string, the error will remain.

多行字符串,错误仍然存​​在。

Please look into this matter:

请看一下这个问题:

<!-- start: definition-->
@{
    dynamic item = new System.Dynamic.ExpandoObject();
    item.MultiLineString = @"a multi-line
                             string";
    item.SingleLineString = "a single-line string";
}
<!-- end: definition-->
<a href="#" onclick="Getinfo('@item.MultiLineString')">6/16/2016 2:02:29 AM</a>
<script>
    function Getinfo(text) {
        alert(text);
    }
</script>

Change the single-quote(') to backtick(`) in Getinfo as bellow and error will be fixed:

将 Getinfo 中的单引号(')更改为反引号(`),如下所示,错误将得到修复:

<a href="#" onclick="Getinfo(`@item.MultiLineString`)">6/16/2016 2:02:29 AM</a>

回答by cspete

I also had an issue with multiline strings in this scenario. @Iman's backtick(`) solution worked great in the modern browsers but caused an invalid character error in Internet Explorer. I had to use the following:

在这种情况下,我也遇到了多行字符串的问题。@Iman 的反引号(`) 解决方案在现代浏览器中运行良好,但在 Internet Explorer 中导致无效字符错误。我不得不使用以下内容:

'@item.MultiLineString.Replace(Environment.NewLine, "<br />")'

Then I had to put the carriage returns back again in the js function. Had to use RegEx to handle multiple carriage returns.

然后我不得不将回车再次放回js函数中。不得不使用 RegEx 来处理多个回车。

// This will work for the following:
// "hello\nworld"
// "hello<br>world"
// "hello<br />world"
$("#MyTextArea").val(multiLineString.replace(/\n|<br\s*\/?>/gi, "\r"));