简体   繁体   English

Javascript错误:'缺少'参数列表后“

[英]Javascript Error: 'missing ) after argument list"

I am making an image for my webpage through javascript like so: 我通过javascript为我的网页制作image

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 ShowImagePopUP I 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? 有什么建议?

You need to wrap the contents of ShowImagePopUP in quotes: 您需要将ShowImagePopUP的内容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/

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;
}

The value in ImageArray[x].src_big needs to be quoted. 需要引用ImageArray[x].src_big的值。

Try to avoid building HTML by mashing strings together. 尽量避免通过将字符串混合在一起来构建HTML。 Using a DOM builder gives code that is much easier to debug. 使用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。

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. 然后,您可以使用调试器找出您的'或'不平衡的位置。当您拥有它时,您可以将它合并为一行,如果您需要或将其与中间变量保持一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM