【问题标题】:Load multiple files into one div via JQuery .load?通过 JQuery .load 将多个文件加载到一个 div 中?
【发布时间】:2016-06-18 18:42:14
【问题描述】:

我正在尝试这样做:

$("#Div").click(function(){
  $("#PageContentDiv").load("file.php");
  $("#PageContentDiv").load("another.php");
});

这个问题是第二个加载函数正在替换第一个......

【问题讨论】:

  • 如果你想保持东西井井有条,最容易加载到单独的 div 中。

标签: jquery jquery-load


【解决方案1】:

尝试在complete 回调中使用$.get() .load() , .append()

$("#Div").click(function() {
  var page = $("#PageContentDiv");
  page.load("file.php", function() {
    $.get("another.php", function(html) {
      page.append(html);
    });
  });     
});

【讨论】:

  • 奇怪的是,这将如何处理 3 个或更多源文件?
  • @faalbane 您可以使用$.when($.get(), $.get(), $.get()) 加载所有文件,.append() 对.then(function(a, b, c) {page.append(a[0], b[0], c[0])}) 元素的每个响应
  • $.when 函数是否发生在 page.load 函数中?你能用一个例子更新你的回复吗?
  • @faalbane 你用 .load() 代替 $.get() , jsfiddle jsfiddle.net/ukqceuL7 ;见doc.jsfiddle.net/use/echo.html
  • 出现错误...我有: $(document).ready(function(){ $("#Div").click(function(){ var request = function(val) { return $.post("/echo/html/", {html:"
    " + val + "
    "}) } $.when(request("file1.php"), request("file2. php"), request("file3.php")) .then(function(a, b, c) { $("#PageContentDiv").append(a[0], b[0], c[0]) }) }); });
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多