我正在创建一个基本论坛,其中每条消息都包含作者姓名,一些文本及其创建日期.我希望论坛能够通过AJAX不断更新,并显示即时创建的新帖子.我目前有一个PHP文件getlatest.php?lastid=...,可以从最新的ID中检索所有帖子.它返回HTML中的数据,就像这样(我已经改变它,所以你可以看到div,stackoverflow将它们抛出):
foreach ($print as $value)
{
$readyText .= div id = $value->post_id;
$readyText .= $value->first_name.' '.$value->last_name.' posted the following:'.
$value->post_text.' The post was made about '.$time.' ago.
$readyText .= '/div>';
}
我在jquery中有一些AJAX代码可以每隔一段时间检索一次
setInterval("update()", 3000);
function update()
{
$.get("getlatest.php",
{
id: latestmessage
},
function(response){
$("#forum_entries").prepend(response);
latestmessage = $.cookie('last_post_id'); //This is
//how I know what the latest post id is
}, "html");
Run Code Online (Sandbox Code Playgroud)
我想突出显示使用(现在非常流行的)黄色渐变技术提交的所有新帖子,就像这样
$("#somediv").effect("highlight", {}, 1500);
Run Code Online (Sandbox Code Playgroud)
我的问题是 - 我应用这个效果的div是什么?我应该在PHP中添加它,每个论坛帖子都有一个div id,实际上它是数据库中的PK.
更改您的函数,以便使用prependTo而不是使用prepend .PrependTo将返回前置的元素,您可以将突出显示应用于这些元素(使用jQuery 1.3.2).
$.get('getlatest.php',
{ id: latestmessage },
function(response) {
$(response).prependTo('#forum_entries').effect('highlight',{},1500);
latestmessage = $.cookie('last_post_id');
}, 'html' );
Run Code Online (Sandbox Code Playgroud)