我正在用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);
(我不想加载整个。产品包含一次,因为部分内容正在由用户同时编辑)
但它为相同的内容向服务器发出两个请求。
我可以用一个请求来完成吗?
谢谢!
你可以做一个 $.get() 然后将内容加载到自己的位置,如下所示:
$.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() .
.html()
.empty().append()