jQuery的:追加()对象,删除()它的延迟() [英] jQuery: append() object, remove() it with delay()

查看:173
本文介绍了jQuery的:追加()对象,删除()它的延迟()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有什么错呢?

$('body').append("<div class='message success'>Upload successful!</div>");
$('.message').delay(2000).remove();

我要追加一个成功的消息,我的html文件,但只有2秒。
在这以后,DIV应该再次删除。

I want to append a success message to my html document, but only for 2sec. After that the div should be deleted again.

我在做什么错在这里?

关于

推荐答案

使用 的setTimeout() 直接(其中 .delay() 内部使用)是简单的在这里,因为 一个.remove() 不是排队功能,总体来说应该是这样的:

Using setTimeout() directly (which .delay() uses internally) is simpler here, since .remove() isn't a queued function, overall it should look like this:

$('body').append("<div class='message success'>Upload successful!</div>");
setTimeout(function() {
  $('.message').remove();
}, 2000);

你可以试试看这里

.delay() 是为动画(或任何命名)队列,使用它你必须做这样的事情:

.delay() is for the animation (or whatever named) queue, to use it you'd have to do something like:

$("<div class='message success'>Upload successful!</div>").appendTo('body')
  .delay(2000).queue(function() { $(this).remove(); });

其中一期工程,这里 ......但仅仅是矫枉过正和非常低效的,海事组织链的缘故。通常情况下,你还不得不打电话出队或下一个功能为好,但因为你是无论如何删除元素...

Which works, here...but is just overkill and terribly inefficient, for the sake of chaining IMO. Normally you'd also have to call dequeue or the next function as well, but since you're removing the element anyway...

这篇关于jQuery的:追加()对象,删除()它的延迟()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆