-->

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

2019-03-02 07:00发布

问题:

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");

回答1:

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');


回答2:

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
});


回答3:

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");