Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery.ajax函数中的html语法错误?_Javascript_Jquery_Ajax - Fatal编程技术网

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

Javascript jQuery.ajax函数中的html语法错误?,javascript,jquery,ajax,Javascript,Jquery,Ajax,我正在操作$.ajax函数中的jSON数组,对它的语法感到困惑。有一个错误,无法找到它 if(value['newVar'] === 1) { $productContainer.append("<div id='productBox' class='grid_3'>\n\ <a href='product.jsp?id="+value['id']+"'><img src='"+va

我正在操作$.ajax函数中的jSON数组,对它的语法感到困惑。有一个错误,无法找到它

 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>");

             }  
if(值['newVar']==1)
{
$productContainer.append(“\n\

\n\
\n\ 通过“+value['company']+”
RS.“+value['price']+”\n\

; }
错误出现在if块和
RS.“+value['price']+”\n\
后的第4行,它在参数列表后表示缺少语句
Uncaught SyntaxError:missing)。我认为错误在于我编写的
onclick=“foo(this)”


有什么帮助吗?

用单引号替换双引号。您正在使用双引号终止字符串并创建语法错误:

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

该错误是由于最后一行的报价错误:
onclick=“foo(this)”

您还缺少两个单引号…
(在字符串的某个地方…并没有导致实际的错误,但稍后生成的HTML中会出现一些错误)

您可以使用您选择的引号作为字符串包装来附加…
另一个用于字符串内部。

但你必须注意不要“混合”它们! 一个是字符串包装器,另一个是字符串的一部分

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>");

}
if(值['newVar']==1){
$productContainer.append(“\n”+
“
\n”+ “
\n”+ “通过“+value['company']+”
RS.“+value['price']+”\n”+ “

”; }
技巧:

  • 使用
    +
    符号连接行。
    它防止在字符串中包含多个制表符和空格。
    这比你在每一行末尾用斜杠来拖尾要好
  • 使您的变量更加直观,以便能够更好地看到它们,就像我上面所做的那样。
    它也有助于检查缺失或误用的引用

如果您正在执行多行操作,请连接每一行。@Roljhon如何连接`\我想这个接线员是为我做的对不起,我没注意到:)