gpt4 book ai didi

php - 通过 AJAX 更新呈现页面的所有元素的正确方法是什么?

转载 作者:行者123 更新时间:2023-12-04 06:57:46 25 4
gpt4 key购买 nike

我有一个由 PHP 呈现的复杂页面,并希望通过 AJAX 长轮询使页面的所有元素保持最新。是否有某种通用/聪明的方法来设计基础设施来支持这一点,而无需手动指定要更新的每个元素?只是在寻找想法。谢谢!

最佳答案

使用 jQuery,我会发送一个逗号分隔的 jQuery 选择器列表来更新到服务器。服务器最终将通过读取这些选择器并生成 HTML 来填充与选择器匹配的元素来做出响应:

$.get("/updater", { elementsToUpdate: "#someDiv,#someTable,#someOtherElement"}, function(json) {
$.each(json, function(k, v) {

// the key is the selector, the value is the
// HTML to set to that (or those) element(s):
$(k).html(v);
});
}, "json"); // we are expecting the server to return JSON

服务器将通过向客户端发送具有以下结构的 JSON 来响应:
 {"#someDiv":"this is some HTML to fill up div with ID someDiv","#someOtherElement":"here is some more HTML","#someTable":"here is some more HTML"}

关于php - 通过 AJAX 更新呈现页面的所有元素的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2342930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com