通过 AJAX 更新渲染页面的所有元素的正确方法是什么?

发布于 2024-08-22 17:13:33 字数 106 浏览 11 评论 0原文

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

I have a complicated page being rendered by PHP and would like to keep all elements of the page up to date via AJAX long polling. Is there some kind of general / clever way to design an infrastructure to support this without having to specify manually each element to update? Just looking for ideas. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

水波映月 2024-08-29 17:13:33

使用 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"}

Using jQuery, I would send a comma-separated list of jQuery selectors to update to the server. The server will ultimately respond by reading those selectors and producing the HTML to fill up the elements matching the selectors:

$.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

The server will respond by sending JSON to the client with the following structure:

 {"#someDiv":"this is some HTML to fill up div with ID someDiv","#someOtherElement":"here is some more HTML","#someTable":"here is some more HTML"}
硬不硬你别怂 2024-08-29 17:13:33

也许向所有要通过ajax更新的元素添加一个特殊的类,并且您还可以在其他一些属性中编码额外的数据,例如

<a class="ajaxUpdate" data="{'a':'json or whatever', 'put whatever here':'ok'}">test</a>

使用jquery,您可以轻松地提取该数据并对其进行评估,然后在ajax中使用它

maybe add a special class to all the elements to be updated via ajax, and also you can encode extra data in some other attributes for example

<a class="ajaxUpdate" data="{'a':'json or whatever', 'put whatever here':'ok'}">test</a>

then with jquery you can easily pull out that data and eval it, and use it in your ajax

悸初 2024-08-29 17:13:33

您可以轮询一个页面,该页面会返回页面的 json 编码结构,例如:

var page = {
    'elem1': {
        'html': '<div>... ',
        'update': True
    },
    'elen2': {
        'update': False

等等,然后更新必要的内容,仅进行一次轮询。

You can poll a page which returns you the json-encoded structure of your page, like:

var page = {
    'elem1': {
        'html': '<div>... ',
        'update': True
    },
    'elen2': {
        'update': False

and so on, then update what necessary, having only one poll.

無心 2024-08-29 17:13:33

将页面元素结构保留为私有数据,并使用公共元素来更新它们

var page = (function(){
    var private_data = {
    //json
    };

    return {
        workOnData : function(){//process the data}

    }
})()

这是保持页面数据安全和流畅的好方法。

Keep your pages elements structure as private data and have the public elements to update them

var page = (function(){
    var private_data = {
    //json
    };

    return {
        workOnData : function(){//process the data}

    }
})()

This is good way you can keep your pages data safe and smooth.

黄昏下泛黄的笔记 2024-08-29 17:13:33

如果我是你,我会使用 jQuery 来做到这一点:那么你可以做类似的事情......

$('div.updateThisClass').css('color','#fff')

I'd use jQuery to do that if I were you o: then you could do something like...

$('div.updateThisClass').css('color','#fff')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文