代码之家  ›  专栏  ›  技术社区  ›  Pavel K.

使用jquery.load()一次加载多个片段

  •  0
  • Pavel K.  · 技术社区  · 14 年前

    我正在用jquery加载一些片段,代码如下

    var box = $(this).closest(".product-contain").children(".title:first");
    box.load(url + " .maintitle", data);
    var box = $(this).closest(".product-contain").children(".detail:first");
    box.load(url + " .alldetail", data);
    

    (我不想加载整个。产品包含一次,因为部分内容正在由用户同时编辑)

    但它为相同的内容向服务器发出两个请求。

    我可以用一个请求来完成吗?

    谢谢!

    1 回复  |  直到 14 年前
        1
  •  3
  •   Nick Craver    14 年前

    你可以做一个 $.get() 然后将内容加载到自己的位置,如下所示:

     var pc = $(this).closest(".product-contain");
     $.get(url, data, function(html) {
       var resp = $(html);
       pc.children(".title:first").html(resp.find(".maintitle"));
       pc.children(".detail:first").html(resp.find(".alldetail"));
     });
    

    这看起来有点奇怪,但是 .html() 使用jquery对象是 .empty().append() .

    推荐文章