使用 Ajax 加载资源然后替换 #listing
和 #contextActions
的内容的最有效方法是什么?
// Load resource and replace source `listing` with target `listing`.
$('#listing').load('/myuri.php #listing');
// Load resource and replace source `contextActions` with target `contextActions`.
$('#contextActions').load('/myuri.php #contextActions');
肯定有更好的方法吗?我不喜欢在同一个资源上有两个加载请求的想法!
最佳答案
有一个更好的方法可以做到这一点。
让/myuri.php
返回JSON 对象,其中包含 #listing
和 #contextActions
所需的数据,并使用回调来分配它。
$.load('/myuri.php', {}, function (responseText, textStatus, XMLHttpRequest) {
var data = $.parseJSON(responseText);
$('#listing').html(data.listing);
$('#contextActions').html(data.contextActions);
});
关于javascript - 使用 jQuery 加载多个片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7065299/