javascript - 使用 jQuery 加载多个片段

标签 javascript jquery ajax xmlhttprequest

使用 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/

相关文章:

javascript - Userscript 仅适用于从后端提供的页面,但不适用于 SPA 方式的前端

javascript - 使用 javascript 创建多个 if/else 条件

javascript - 专注于嵌套的 contenteditable 元素

jquery - 使用从服务器接收到的 json 值更新多个 div 内容

jquery 应用 css 标签

javascript - jquery中的 ".on"函数错误

jquery - rails : how to update a has_many :through relation via jQuery?

javascript - W3Schools AJAX 示例无法在本地计算机上运行

javascript - 从 API 获取数据并使用 jQuery 将其分离到变量中

javascript - 如何使用javascript修改gridview单元格值