[英]How to show use jQuery load() to ajax multiple items into multiple elements?
我需要將大頁面中的某些項目加載到頁面的不同元素中。 我編寫的代碼可以正常工作,但項目會一次又一次且經過大量暫停后加載。 我想我做錯了方法,因為我不是一個完整的開發人員,而只是一個設計師。
我的代碼看起來像:
$("#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");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.