jQuery在一个元素中加载多个html文件

its*_*sme 5 html javascript jquery load

如何加载多个html文件并将它们放入指定的html元素?

我尝试了没有变化:

$('#asd').load('file.html,pippo.html');
Run Code Online (Sandbox Code Playgroud)

use*_*654 7

试试这个,使用延迟对象。

var defArr = [];
defArr.push($.get('file.html'));
defArr.push($.get('pippo.html'));
$.when.apply($,defArr).done(function(response1,response2){
    $('.result').html(response1[2].responseText + response2[2].responseText);
});
Run Code Online (Sandbox Code Playgroud)


ear*_*tom 5

你可以得到多个项目并将它们添加到元素中.

jQuery.ajaxSetup({ async: false }); //if order matters
$.get("file.htm", '', function (data) { $("#result").append(data); });
$.get("pippo.htm", '', function (data) { $("#result").append(data); });
jQuery.ajaxSetup({ async: true });  //if order matters
Run Code Online (Sandbox Code Playgroud)

  • @gl3nn 使用 `async:false` 在请求期间锁定浏览器,并导致请求按顺序而不是同时发送,可能导致获取所有请求的 html 需要更长的时间(导致前面提到的锁持续更长时间)。 (2认同)