【问题标题】:selecting divs retrieved through AJAX in jquery在 jquery 中选择通过 AJAX 检索的 div
【发布时间】:2010-11-07 07:20:41
【问题描述】:

我正在创建一个基本论坛,其中每条消息都包含作者姓名、一些文本和创建日期。我希望论坛通过 AJAX 不断更新,并显示动态创建的新帖子。 我目前有一个 PHP 文件getlatest.php?lastid=...,它可以从提供给最新 ID 的 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");

我想突出显示所有使用(现在非常流行的)黄色淡化技术提交的新帖子,就像这样

$("#somediv").effect("highlight", {}, 1500);

我的问题是 - 我将这个效果应用于哪个 div? 我应该在 PHP 中补充一下,每个论坛帖子都有一个 div id,它实际上是它在数据库中的 PK。

【问题讨论】:

    标签: php jquery css html


    【解决方案1】:

    在循环中,您可以将假类属性添加到较新的帖子 div... 然后在添加到#forum_entries 之后在您的ajax 调用中 您可以应用突出显示,在 Jquery 中通过 removeAttr(classname) 删除类属性。 所以下次执行就没有问题了。

    【讨论】:

      【解决方案2】:

      更改您的函数,使其使用prependTo,而不是使用前置。 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' );
      

      【讨论】:

        【解决方案3】:

        给你的 Div 一个活跃的类:

        <?php
        
        foreach ($print as $value)
        {
            $readyText .= '<div id = "' . $value->post_id . '" class="active"';
            $readyText .= $value->first_name.' '.$value->last_name.' posted the following:'.
            $value->post_text.' The post was made about '.$time.' ago. 
            $readyText .= '</div>'; 
        }
        
        ?>
        
        
        
        setInterval("update()", 3000);
                    function update()
                        {
                        $.get("getlatest.php", 
                                {
                            id: latestmessage
                        }, 
                                function(response){
                            $("#forum_entries").prepend(response);
                            latestmessage = $.cookie('last_post_id');
                    $("div.active").effect("highlight", {}, 1500);
                    $("div.active").toggleClass("active");
        
                        }, "html");
        

        正如我在回答您之前的问题时已经建议的那样,结合流行的库/框架之一学习一点 JavaScript 真的很有意义(我推荐上面的示例 jQuery使用)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-06-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-02-05
          • 1970-01-01
          相关资源
          最近更新 更多