如何显示使用 jQuery load() 将多个项目转换为多个元素

How to show use jQuery load() to ajax multiple items into multiple elements?

本文关键字:项目 转换 元素 jQuery 何显示 显示 load      更新时间:2023-09-26

我需要将某些项目从大页面加载到页面的不同元素中。我编写的代码正在工作,但项目在大量暂停后逐个加载。我想我可能会做错事,因为我不是一个完整的开发人员,而只是一个设计师。

我的代码看起来像:

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

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

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

为什么不使用$.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
});

试试这个

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