Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.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
jQuery追加函数的行为是异步的_Jquery - Fatal编程技术网

jQuery追加函数的行为是异步的

jQuery追加函数的行为是异步的,jquery,Jquery,我有这个密码 $("#addr").append("<ul>"); $(data.customer).each( function() { $("#addr").append("<li><div><b><i><u> CUSTOMER ID </u></i></b>: "+this.custId+"<br>"+ "&

我有这个密码

$("#addr").append("<ul>");
$(data.customer).each( function() {
    $("#addr").append("<li><div><b><i><u>
                CUSTOMER ID </u></i></b>: "+this.custId+"<br>"+
            "<b><u>customer Phone</u></b>"+this.custPhone+"<br>");
    $("#addr").append("</div></li></br></br>");         
});

$("#addr").append("<p>hiiielajekf</p></ul>");
$(“#addr”)。追加(“
    ”); $(data.customer)。每个(函数(){ $(“#addr”).append(
  • 客户ID:“+this.custId+”
    ”+ “客户电话”+此.custPhone+“
    ”; $(“#addr”)。追加(“


  • ”); }); $(“#addr”).append(“hiiielajekf

”);
但在我的浏览器中,
是空的,所有列表项都从
之后开始


有人能指出我的错误吗?

你在用标记思考,但这不是你要处理的。您正在处理一个对象树<代码>$(“#addr”).append(“
    ”)创建并追加一个
    ul
    元素,而不仅仅是开始一个。类似地,稍后在一个
    append
    中有
  • ,在另一个
  • 中有一个
    ,这不是DOM(和jQuery)的工作方式。传递到
    append
    中的每个字符串必须是一个完整、格式良好的HTML片段(具有平衡的开始和结束标记),但如果只执行
    .append(“”
    ),jQuery会将其视为执行了
    .append(“”

    要么将整个内容构建为一个字符串,然后一次性将其追加,要么处理对象

    假设
    data.customer
    是一个数组(出于某种原因,您需要坚持使用ES5级别的功能),则执行一个大字符串:


    你在考虑标记,但这不是你要处理的。您正在处理一个对象树<代码>$(“#addr”).append(“
      ”)
    创建并追加一个
    ul
    元素,而不仅仅是开始一个。类似地,稍后在一个
    append
    中有
  • ,在另一个
  • 中有一个
    ,这不是DOM(和jQuery)的工作方式。传递到
    append
    中的每个字符串必须是一个完整、格式良好的HTML片段(具有平衡的开始和结束标记),但如果只执行
    .append(“”
    ),jQuery会将其视为执行了
    .append(“”

    要么将整个内容构建为一个字符串,然后一次性将其追加,要么处理对象

    假设
    data.customer
    是一个数组(出于某种原因,您需要坚持使用ES5级别的功能),则执行一个大字符串:

    $("#addr").append(
        "<ul>" +
        data.customer.map(function(customer) {
            return "<li>" +
                "<div>" +
                    "<b><i><u> CUSTOMER ID </u></i></b>: " +
                    this.custId + "<br>" +
                    "<b><u>customer Phone</u></b>: " +
                    this.custPhone + "<br>"
                "</div>" +
            "</li>;
        }) +
        "</ul>"
    );
    $("#addr").append("<p>hiiielajekf</p></ul>");
    
    var ul = $("<ul>");
    data.customer.forEach(function(customer) {
        ul.append("<li>" +
                "<div>" +
                    "<b><i><u> CUSTOMER ID </u></i></b>: " +
                    this.custId + "<br>" +
                    "<b><u>customer Phone</u></b>: " +
                    this.custPhone + "<br>"
                "</div>" +
            "</li>");
    });
    $("#addr").append(ul);
    $("#addr").append("<p>hiiielajekf</p></ul>");