简体   繁体   English

jQuery.ajax函数中的html语法错误?

[英]html syntax error in jQuery.ajax funciton?

I am manipulating the jSON array in the $.ajax function and i got confused with its syntax. 我在$ .ajax函数中操纵jSON数组,但对其语法感到困惑。 There is a error and can't find it. 存在错误,找不到它。

 if(value['newVar'] === 1)
              {
              $productContainer.append("<div id='productBox' class='grid_3'>\n\
             <a href='product.jsp?id="+value['id']+"'><img src='"+value["image"]+"'/></a><br/>\n\
             <a href='product.jsp?id="+value['id']+"'><span class='black'>"+value['name']+"</span></a><br/>\n\
             <span class='black'>By "+value['company']+"</span><br/><span class='red'>RS."+value['price']+"</span>\n\
             <br/><br/><a href='#' onclick="foo(this)" pid="+value['id']+">REMOVE</a></div>");

             }  

Error is in the if block and in the 4th line after RS."+value['price']+"</span>\\n\\ it says missing statement Uncaught SyntaxError: missing ) after argument list . 错误发生在if块中,并位于RS."+value['price']+"</span>\\n\\后的第四行RS."+value['price']+"</span>\\n\\它表示Uncaught SyntaxError: missing ) after argument list缺少语句Uncaught SyntaxError: missing ) after argument list I think the error is in the way I have written onclick="foo(this)" 我认为错误是我写onclick="foo(this)"

Any help? 有什么帮助吗?

Replace the double quotes with single quotes. 用双引号替换双引号。 You are terminating the string with the double quotes and creating a syntax error: 您用双引号终止字符串并创建语法错误:

   ...<a href='#' onclick='foo(this)' pid='"+value['id']+"'>

The error was due to a quote misuse on the last line: onclick="foo(this)" . 该错误是由于最后一行的引号滥用: onclick="foo(this)"
You also had two single quotes missing... 您还缺少两个单引号...
(inside the string somewhere... Not causing the actual error, but you would had some errors in the produced HTML later). (在字符串中的某处...不会引起实际错误,但是稍后在生成的HTML中会出现一些错误)。

You can use the quote of your choice as the string wrapper to append... 您可以使用您选择的引号作为要附加的字符串包装器...
And the other for inside the string. 而另一个用于字符串内部。

But you have to watch out for not "mixing" them! 但是您必须提防不要“混合”它们! One is the string wrapper and the other is part of the string. 一个是字符串包装器,另一个是字符串的一部分。

if(value['newVar'] === 1){
  $productContainer.append("<div id='productBox' class='grid_3'>\n"+
                           "<a href='product.jsp?id='" +value['id']+ "'><img src='" +value["image"]+ "'/></a><br/>\n"+
                           "<a href='product.jsp?id='" +value['id']+ "'><span class='black'>" +value['name']+ "</span></a><br/>\n"+
                           "<span class='black'>By " +value['company']+ "</span><br/><span class='red'>RS." +value['price']+ "</span>\n"+
                           "<br/><br/><a href='#' onclick='foo(this)' pid=" +value['id']+ ">REMOVE</a></div>");

}

Tricks: 技巧:

  • Concatenate your lines using the + sign. +号连接您的行。
    It prevent the inclusion of multiple tabs and spaces inside the string. 它可以防止在字符串中包含多个制表符和空格。
    It is a better practice than your trailling slash \\ at the end of each line. 比起每行末尾的斜杠\\ ,这是一种更好的做法。
  • Make your variables more evident visually, to be able to better see them, like I did above. 像我上面所做的那样,使变量在视觉上更加明显,以便能够更好地查看它们。
    It also helps checking for missing or misused quotes. 它还有助于检查报价是否丢失或误用。

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

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