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

问题描述

这是怎么回事?

$('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() 不是't 一个排队的函数,总体上应该是这样的:

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(); });

哪个有效,在这里...但只是矫枉过正,效率极低,对于为了链接 IMO.通常,您还必须调用 dequeue 或 next 函数,但由于您无论如何都要删除元素...

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:append() 对象,使用 delay() 删除()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 04:46