【问题标题】:Highlighting a newly added item using jQuery使用 jQuery 突出显示新添加的项目
【发布时间】:2010-11-12 13:16:32
【问题描述】:

当用户将新评论 (divCommentHtml) 添加到 cmets (divComments) 列表中时,我想将其突出显示几秒钟然后将其淡出。你怎么能用jquery做到这一点?这似乎不起作用:

 $('#divComments').prepend($divCommentHtml).effect("highlight", {}, 2000);  

谢谢!

【问题讨论】:

  • effect 函数是什么?
  • 5 秒内你可以文本装饰:下划线和删除之后。你可以使用 settimeout 和 cleartimeout 函数来做同样的事情
  • 另外,这似乎是this question的副本
  • 其实,也许不是:\

标签: javascript jquery


【解决方案1】:

使用prepend,您在#divComments 元素上运行UI 效果。

试试这个(假设 $divCommentHtml 是一个 jQuery 对象)

$divCommentHtml.prependTo('#divComments').effect('highlight', {}, 2000);

或许

$('#divComments').prepend($divCommentHtml)
                 .find(':first').effect('highlight', {}, 2000);

【讨论】:

    【解决方案2】:

    jQuery 没有开箱即用的effect()更新哦,这是一个 jQuery UI 的东西。继续:)

    $divCommentHtml.prepentTo('#divComments').effect("highlight", {}, 2000);
    

    我更改了它的工作方式,因此您的方法正在实际的 $divCommentHtml 对象上运行。

    【讨论】:

      【解决方案3】:

      另一种选择是给 $divCommentHtml 一个 ID,然后在添加后运行效果

      $divCommentHtml = '<div id="myID">Some Contents</div>'
      $('#divComments').prepend($divCommentHtml);    
      $( "#myID" ).effect( 'highlight', {}, 2000);
      

      【讨论】:

        【解决方案4】:

        divCommentHtml 周围缺少括号。检查Live demo

        $('#divComments').prepend($(divCommentHtml)).effect("highlight", {}, 2000); 
        

        【讨论】:

        • 没必要。它们是括号,而不是大括号。我认为他的意思是实际变量有一个$ sigil。
        • 和 divCommentHtml 似乎是一个变量,而不是一个 jquery 对象
        • 谢谢。但是请检查我给出的带括号和不带括号的链接。没有括号,它认为 $divCommentHtml 是变量[未定义],而有括号则返回 jquery 对象。
        • @Chinmayee 在您的情况下可能未定义。我认为 OP 省略了变量,但我们可以放心地假设它包含一个 jQuery 对象。
        • @Chinmayee 你的代码有错别字s/prepentTo/prependTo/
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-26
        • 1970-01-01
        • 2019-03-12
        • 2013-02-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多