0

我正在开发 AJAX/jquery 通知脚本。
目前,它在 AJAX 响应中每 10 秒返回一个完整的 fhtml 格式页面。
它返回的页面是一个 PHP 页面,它仅显示应显示的项目(仅显示具有新内容的项目,如新邮件或新评论等。)

我想将其更改为使用 JSON,但在我的主页(父级)上,我将为每个通知项设置 DIV,默认情况下,它们将使用 CSS 隐藏,JSON 响应将告诉我应该取消隐藏哪些项。

这就是我的基本计划,下面是一些视觉模型代码。

JSON 响应,在 10 个可能的项目中,它只会返回标有 1 的项目(表示是显示该项目)
甚至可能不需要 1,因为我只显示已经确认显示的项目php?

{"mail":"1", "friend_request":"1" , "comment":"1" , "photo_comment":"1"},

在主父页面上,将有带有 CSS 的 DIV,使它们像这样隐藏。(演示只有 4 个项目)

<style type="text/css">
#mail_notification{
    display: none;
}
#friend_request_notification{
    display: none;
}
#comment_notification{
    display: none;
}
#photo_comment_notification{
    display: none;
}
</style>

<div id="mail_notification"><a href="someurl.com/mail.php?id2424">New Mail</a></div>
<div id="friend_request_notification"><a href="someurl.com/mail.php?id2424">New Friend Request</a></div>
<div id="comment_notification"><a href="someurl.com/mail.php?id2424">New Profile COmments</a></div>
<div id="photo_comment_notification"><a href="someurl.com/mail.php?id2424">New Photo Comments</a></div>

那么有人可以告诉我我会怎么做吗?


这是我的 CUREENT 代码,用于显示 ajax 通知,使用 OLD 方法,它还没有使用 JSON

<!-- Auto update/look for NEW notifications -->
<script type='text/javascript'> 
$(document).ready(function(){ 
     var updatenotification = function(){ 
          $('#notificationcontainer') 
               .load('/modules/member/home/notifications.inc.php') 
               .fadeIn("slow"); 
     }; 
     var auto_refresh = setInterval(function(){updatenotification();}, 5000); 
     updatenotification(); 
}); 
</script>
<!-- ENDS HERE -->
4

1 回答 1

4

将更新通知消息更改为使用 get 而不是 load,并让回调遍历 JSON 键,显示与键对应的值为 1 的那些 DIV。

var updatenotification = function() {
  $('#notificationcontainer')
    .get('/modules/member/home/notifications.inc.php', null, function(data) {
      for (key in data) {
        if (data[key]) {
          $('#' + key + '_notification').fadeIn();
        }
      }
    });
};
于 2009-08-30T02:18:48.130 回答