Javascript 错误“缺少;在语句之前”

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

Javascript Error "Missing ; before statement"

javascript

提问by Rolando Cruz

I am having a very weird error on this block:

我在这个块上有一个非常奇怪的错误:

<script type="text/javascript">
  var captions = new Array();
  captions[0] = "";
  captions[1] = '<div class="cap-desc"><h3>No Win - No Fee</h3><span class="captionSubhead">Performance Guarantee</span><br /><span style="color:#636363; font-size:13px;">Ask Today. We are very confident<br />you'll be impressed with our results! </span></div><br /><a href="http://#" class="rmore" target="_blank">read more</a>';
</script>

The error tells me that I am missing a ';' before captions[1] but I am pretty sure that captions[0] has a semi-colon!

错误告诉我我缺少一个 ';' 在 captions[1] 之前,但我很确定 captions[0] 有一个分号!

The whole site is here: http://katron.sourcefit.com/cms02/a&a/

整个网站在这里:http: //katron.sourcefit.com/cms02/a&a/

It has 5 captions now, but same error nonetheless. I tried changing the contents of the caption in question. In this case, it's captions[1] and it works. What is wrong with the caption? I am sure I am closing all tags and quotes and escaping \n to

它现在有 5 个字幕,但仍然存在相同的错误。我尝试更改相关标题的内容。在这种情况下,它是标题 [1] 并且它有效。字幕有什么问题?我确定我正在关闭所有标签和引号并转义 \n 到

回答by Quintin Robinson

You are not escaping a single quote properly...

您没有正确转义单引号...

captions[1] = '<div class="cap-desc"><h3>No Win - No Fee</h3><span class="captionSubhead">Performance Guarantee</span><br /><span style="color:#636363; font-size:13px;">Ask Today. We are very confident<br />you\'ll be impressed with our results! </span></div><br /><a href="http://#" class="rmore" target="_blank">read more</a>';

This just escapes the apostrophe in

这只是逃避了撇号

you'll

你会

回答by jkeesh

The problem is you'llhas an apostrophe and ends the code block. Make sure to escape it.

问题是you'll有一个撇号并结束了代码块。确保逃脱它。

回答by Ben

Found this question when I was googling the error.

我在谷歌搜索错误时发现了这个问题。

My solution is not relevant to the original post but may help someone anyway.

我的解决方案与原始帖子无关,但无论如何可能会帮助某人。

I had forgotten the word functionafter a declaration:

function在声明后忘记了这个词:

Foo.bar(evt) {
    ...
}

should have been

本来应该

Foo.bar = function(evt) {
    ...
}

Took me a little while to find this bug, so hopefully it helps someone.

我花了一点时间才找到这个错误,所以希望它可以帮助某人。

回答by Rajesh Kumar K V

The same error can occur if you are trying to add a property to an existing object prefixing with a var. For example

如果您尝试将属性添加到以 var 为前缀的现有对象,则可能会发生相同的错误。例如

var existingObject = {};

var existingObject.property1 = {}; //will throw the same error

The solution is:

解决办法是:

var existingObject = {};

existingObject.property1 = {}; //without prefixing with var

回答by Prashant Lakhlani

That is because of single quote in last line at confident
you'll be impressed
repalce it with \'and it think that will fix it

那是因为最后一行中的单引号将其重新放置,并且它认为可以解决它confident
you'll be impressed
\'

回答by BZink

you'll be impressed with our results!

我们的结果会给您留下深刻印象!

That apostrophe (') in you'll is ending your string. So the javascript engine is looking for a semicolon there.

you'll 中的撇号 (') 将结束您的字符串。所以 javascript 引擎正在那里寻找一个分号。

replace with \' to escape it

替换为 \' 以逃避它

回答by Ravia

Check "you'll be impressed with our results! " in your captions[1] it has ' character which does not have escape character.

选中“您会对我们的结果印象深刻!”在您的标题[1] 中,它有 ' 字符,但没有转义字符。