-->

如何显示使用jQuery load()来多个项目Ajax的多个元素?(How to show use

2019-07-29 17:35发布

我需要从一个大页面的某些项目加载到页面中的不同元素。 我写的代码是工作,但加载项逐一和大量的停顿之后。 我想我可能会做错误的方式,因为我不是一个完整的开发者,但只是一个设计师。

我的代码看起来喜欢:

$("#lot-rental").load("/est.html #est-rental");
$("#lot-deposit").load("/est.html #est-deposit");
$("#lot-date").load("/est.html #est-date");
$("#lot-build").load("/est.html #est-build");

Answer 1:

使用$.get()加载数据,然后手动设置各种元素的含量。

$.get('/est.html', function(data) {
    $.each(['rental', 'deposit', 'data', 'build'], function(i, key) {
        $('#lot-' + key).html($(data).find('#est-' + key));
    });
}, 'html');


Answer 2:

为什么不使用$.get解析响应(找到的元素),并将它们加载到主画面:

$.get('/est.html',function(html){
    //wrap in jQuery so we can use it as context
    html = $(html);

    //find the item in html, and append to the container in the current page
    $('#est-rental',html).appendTo('#lot-rental');
    //     ^          ^                  ^-- target
    //     |          '-- context
    //     '-- the element to find in HTML
});


Answer 3:

试试这个

$("#lot-rental").load("est.html#est-rental");
$("#lot-deposit").load("est.html#est-deposit");
$("#lot-date").load("est.html#est-date");
$("#lot-build").load("est.html#est-build");


文章来源: How to show use jQuery load() to ajax multiple items into multiple elements?