类似于:Using fadein and append

But the solutions there aren't working for me. I'm trying:

 $('#thumbnails').append('<li><img src="/photos/t/'+data.filename+'"/></li>').hide().fadeIn(2000);

但随后整个列表立即消失,而不是随着每个项目的添加.看起来hide()fadeIn()适用于$('#thumbnails'),而不是<li>.我该如何让他们申请呢?这也行不通:

$('#thumbnails').append('<li stle="display:none"><img src="/photos/t/'+data.filename+'"/></li>').filter(':last').fadeIn(2000);

Other suggestions?

推荐答案

Your first attempt is very close, but remember that append() is returning #thumbnails, not the item you just added to it. Instead, construct your item first and apply the hide().fadeIn() before adding it:

$('#thumbnails')
    .append($('<li><img src="/photos/t/'+data.filename+'"/></li>')
        .hide()
        .fadeIn(2000)
    );

这使用美元函数提前构造<li>.当然,您也可以将其写成两行,如果这样做更清楚的话:

var item = $('<li><img src="/photos/t/'+data.filename+'"/></li>')
    .hide()
    .fadeIn(2000);
$('#thumbnails').append(item);

Edit: Your second attempt is also almost there, but you need to use children() instead of filter(). The latter only removes nodes from the current query; your newly-added item isn't in that query, but is a child node instead.

$('#thumbnails')
    .append('<li style="display:none"><img src="/photos/t/'+data.filename+'"/></li>')
    .children(':last')
    .hide()
    .fadeIn(2000);

Jquery相关问答推荐

jQuery从JSON创建嵌套列表

更改后如何使用 jQuery Select CSS 类

jQuery 或 JavaScript 中的$符号是什么意思?

jQuery 时间 Select 器

Jquery如何通过数组中的属性查找对象

使用 jQuery 时何时/为什么要在变量前加上$?

使用 jQuery 更改 CSS 类属性

jQuery Mobile:动态添加内容的标记增强

使用 jQuery Select 焦点文本在 Safari 和 Chrome 中不起作用

未捕获的 TypeError:data.push 不是函数

虽然未定义变量 - 等待

消除移动 Safari 中点击事件的 300 毫秒延迟

jQuery:如何创建一个简单的叠加层?

通过 :not 在 jQuery Select 器中隐藏除 $(this) 之外的所有内容

5秒后jQuery自动隐藏元素

jQuery empty() 与 remove()

jquery $(window).width() 和 $(window).height() 在未调整视口大小时返回不同的值

.success() 和 .complete() 之间的区别?

jquery追加到列表的前面/顶部

使用 Chosen 插件更改 Select 中的 Select