Javascript 错误:'缺少 ) 参数列表"

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

Javascript Error: 'missing ) after argument list"

javascripthtml

提问by Stanley Cup Phil

I am making an imagefor my webpage through javascript like so:

我正在image通过 javascript 为我的网页制作一个,如下所示:

photoHTMLString = '<li class = "SliderPhoto"><img src =  "' + ImageArray[x].src_small + '" size = "thumb" onclick = "ShowImagePopUP(' + ImageArray[x].src_big + ')" class = "FacebookSliderPhoto"/></li>';

Whenever I try and click a photo go into ShowImagePopUPI get this error:

每当我尝试单击照片进入时,ShowImagePopUP我都会收到此错误:

missing ) after argument list
[Break On This Error] ShowImagePopUp(http://a8.sph...389_84095143389_5917147_2636303_n.jpg)

It doesn't look like I am missing any ')'s so I am lost on the error. Any suggestions?

看起来我没有遗漏任何')',所以我迷失在错误中。有什么建议?

回答by mellamokb

You need to wrap the contents of ShowImagePopUPin quotes:

您需要将 的内容ShowImagePopUP用引号括起来:

"ShowImagePopUp(\'' + ImageArray[x].src_big + '\')"

Which should render as:

应该呈现为:

ShowImagePopUp('http://a8.sph...389_84095143389_5917147_2636303_n.jpg')
               ^ note the quote here

Example: http://jsfiddle.net/V23J6/1/

示例:http: //jsfiddle.net/V23J6/1/

回答by Eineki

try

尝试

photoHTMLString = '<li class = "SliderPhoto"><img src =  "' 
                + ImageArray[x].src_small 
                + '" size = "thumb" onclick = "ShowImagePopUP(\"' 
                + ImageArray[x].src_big + '\")" class = "FacebookSliderPhoto"/></li>';

should do the trick and solve your problem leaving intact the uglyness of you code

应该解决问题并解决您的问题,使代码的丑陋完好无损

A function like this one should be a bit readable and ready to use...

像这样的函数应该有点可读并且可以使用......

function slideElement(image){
    var li=document.createElement('li');
    var img=document.createElement('img');
    li.appendChild(img);
    li.setAttribute('class','SliderPhoto');
    img.setAttribute('class','FacebookSliderPhoto');
    img.setAttribute('size', 'thumb');
    img.setAttribute('src', image.src_small);
    img.setAttribute('onclick', function(){showImagePopUP(image.src_big);});

    return li;
}

回答by Tod

Here's some general advice, build up the strings into intermediate variables and then assemble it at the end. You can then use the debugger to find out where you're getting your ' or "s unbalanced. When you have it all built you can coalesce it into a single line if you want or leave it with the intermediate variables.

这是一些一般性建议,将字符串构建为中间变量,然后在最后进行组装。然后,您可以使用调试器找出 ' 或 " 不平衡的位置。完成所有构建后,您可以根据需要将其合并为一行,或者将其保留为中间变量。

回答by Quentin

The value in ImageArray[x].src_bigneeds to be quoted.

ImageArray[x].src_big需要引用中的值。

Try to avoid building HTML by mashing strings together. Using a DOM builder gives code that is much easier to debug.

尽量避免通过将字符串混合在一起来构建 HTML。使用 DOM 构建器可以让代码更容易调试。

You'd probably be better off writing this so the function computes the large URI based on the small URI rather than having it hard coded.

您可能最好这样写,以便函数根据小 URI 计算大 URI,而不是对其进行硬编码。