How to show use jQuery load() to ajax multiple ite

2019-03-02 07:05发布

I need to load certain items from a big page into a page's different elements. The code I wrote is working but items load one by one and after a lot of pause. I am thinking I might doing it wrong way as I am not a complete developer but just a designer.

My code look likes:

$("#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");

3条回答
戒情不戒烟
2楼-- · 2019-03-02 07:11

Try this

$("#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");
查看更多
闹够了就滚
3楼-- · 2019-03-02 07:21

Use $.get() to load the data and then set the content of the various elements manually.

$.get('/est.html', function(data) {
    $.each(['rental', 'deposit', 'data', 'build'], function(i, key) {
        $('#lot-' + key).html($(data).find('#est-' + key));
    });
}, 'html');
查看更多
一纸荒年 Trace。
4楼-- · 2019-03-02 07:22

Why not use $.get parse the response (find the elements) and load them to the main page:

$.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
});
查看更多
登录 后发表回答