javascript “语法错误:参数列表后缺少)”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17149605/
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
"SyntaxError: missing ) after argument list"
提问by Lulzim
I am getting this error in firebug:
我在萤火虫中收到此错误:
SyntaxError: missing ) after argument list
From this code:
从这个代码:
table +=
'<tbody>'+
'<tr>'+
'<td>'+this.zona+'</td>' +
'<td>'+this.rruga+'<br/><div id="dialog-'+this.id+'" style= "margin-left:1px; width:340px; display:none;height:180px"></div></td>' +
'<td>'+this.dita+'</td>' +
'<td><img src="images/map.jpg" id = "mapicon" onClick="show(this.rruga, this.id);"></td>' +
'<tr>'+
'<tbody>';
I am not quite sure which parenthesis )
is missing in my code.
我不太确定)
我的代码中缺少哪个括号。
回答by Paul S.
This is because of your use of +
with your quotes. You code
这是因为您使用了+
引号。你编码
str = '...' + 'onclick="show(' + this.rruga + ', ' + this.id + ');"' + '...';
will generate HTML that looks like
将生成看起来像的 HTML
onclick="show(hello world,foo bar);"
This obviously won't work, as your function's arguments are no longer quoted properly, so are treated as variable names, then the spaces cause the exception to be thrown. In a worst case scenario, that you do not have this.rruga
or this.id
quoted or HTML encoded, someone could possibly pass a specially crafted piece of code which would inject custom HTML into the page.
这显然不起作用,因为您的函数参数不再正确引用,因此被视为变量名,然后空格会导致抛出异常。在最坏的情况下,您没有this.rruga
或this.id
引用或 HTML 编码,有人可能会传递一段特制的代码,将自定义 HTML 注入页面。
You can keep using a string as long as you encode your arguments so they're HTML-safe and won't end the quotes, and then adding quotes around them them (\'
).
您可以继续使用字符串,只要您对参数进行编码以使它们是 HTML 安全的并且不会结束引号,然后在它们周围添加引号 ( \'
)。
HTML+JavaScript encoded quotes are \\"
for \"
and \\'
for \'
. The preceeding \\
is so that it does not break the JavaScriptquotes, then the &foo;
is the HTML-encoding for the character.
HTML+JavaScript 编码的引号是\\"
for\"
和\\'
for \'
。前面\\
是为了不破坏JavaScript引号,然后&foo;
是字符的 HTML 编码。
str = '...' + 'onclick="show(\''
+ (''+this.rruga).replace(/"/g, '\"').replace(/'/g, '\'') + '\', \''
+ (''+this.id).replace(/"/g, '\"').replace(/'/g, '\'') + '\');"'
+ '...';
As you are generating all of this in JavaScript, consider keeping your listener out of the HTML string and attaching it to the element with addEventListener
or simply node.onclick
. This will mean you can inherit the scope of where you are in the function you want (although this
will still change to mean the Node), and if you use DOM methods you have much less to worry about by way of special encoding as the browser will do the work for you.
当您在JavaScript中生成所有这些时,请考虑将您的侦听器排除在 HTML 字符串之外,并使用addEventListener
或 简单地将其附加到元素node.onclick
。这意味着您可以继承您在所需函数中所在位置的范围(尽管this
仍会更改为表示Node),并且如果您使用 DOM 方法,则您不必担心通过特殊编码的方式,因为浏览器将为你做这项工作。
EDITI'll add a function to make it easy for you
编辑我会添加一个功能,让你很容易
function quoteAndEscape(str) {
return ''
+ ''' // open quote '
+ (''+str) // force string
.replace(/\/g, '\\') // double \
.replace(/"/g, '\"') // encode "
.replace(/'/g, '\'') // encode '
+ '''; // close quote '
}
quoteAndEscape('I\'ll say "hello" to the WORLD!');
// 'I\'ll say \"hello\" to the WORLD!'
// which is a HTML-encoded JavaScript String literal.
// You may also want to encode the opening and closing quotes
// these ones don't need to be backslash-escaped though.
// edited that too
回答by Karoly Horvath
I assume what you posted is part of a command, so there's a preceeding '
.
我假设您发布的内容是命令的一部分,因此前面有一个'
.
You can get this error message if this.rruga
has special characters, for example a space.. eg: the code will create something like show(hello there, 12);
which is syntactically incorrect. You can verify this by just checking the generated html content.
如果this.rruga
有特殊字符(例如空格),您会收到此错误消息。例如:代码将创建类似show(hello there, 12);
语法错误的内容。您可以通过检查生成的 html 内容来验证这一点。
Solution: Put that argument within quotes. ...\''+this.rruga+'\'
...
解决方案:将该参数放在引号内。... \''+this.rruga+'\'
...