几秒后隐藏div
我想知道,在jquery中我如何能够在几秒钟后隐藏div? 例如 Gmail 的消息。
我已尽力但无法使其正常工作。
I was wondering, how in jquery am I able to hide a div after a few seconds? Like Gmail's messages for example.
I've tried my best but am unable to get it working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这将在 1 秒(1000 毫秒)后隐藏 div。
如果您只想隐藏而不褪色,请使用
hide()
。This will hide the div after 1 second (1000 milliseconds).
If you just want to hide without fading, use
hide()
.您可以尝试
.delay()
调用 div 设置延迟时间(以毫秒为单位)并设置您想要更改的属性,在本例中我使用 .fadeOut() 因此它可以是动画的,但是您也可以使用 .hide() 。
http://api.jquery.com/delay/
You can try the
.delay()
call the div set the delay time in milliseconds and set the property you want to change, in this case I used .fadeOut() so it could be animated, but you can use .hide() as well.
http://api.jquery.com/delay/
jquery 提供了多种方法以定时方式隐藏 div,不需要设置和稍后清除或重置间隔计时器或其他事件处理程序。 这里有一些例子。
纯隐藏,一秒延迟
纯隐藏,无延迟
动画隐藏
淡出
此外,这些方法可以将队列名称或函数作为第二个参数(取决于方法)。 上述所有调用和其他相关调用的文档可以在此处找到:
https://api.jquery.com/category/effects/
jquery offers a variety of methods to hide the div in a timed manner that do not require setting up and later clearing or resetting interval timers or other event handlers. Here are a few examples.
Pure hide, one second delay
Pure hide, no delay
Animated hide
fade out
Additionally, the methods can take a queue name or function as a second parameter (depending on method). Documentation for all the calls above and other related calls can be found here:
https://api.jquery.com/category/effects/
有一个非常简单的方法来做到这一点。
问题是 .delay 只影响动画,所以你需要做的是通过给它一个持续时间来使 .hide() 表现得像一个动画。
$("#whatever").delay().hide(1);
通过给它一个较短的持续时间,它看起来就像常规的 .hide 函数一样是即时的。
There's a really simple way to do this.
The problem is that .delay only effects animations, so what you need to do is make .hide() act like an animation by giving it a duration.
$("#whatever").delay().hide(1);
By giving it a nice short duration, it appears to be instant just like the regular .hide function.
来自 http://james.padolsey.com/javascript/jquery-delay-plugin/< /a>
(允许方法链接)
From http://james.padolsey.com/javascript/jquery-delay-plugin/
(Allows chaining of methods)
使用 jQuery 计时器还允许您拥有与附加到对象的计时器相关联的名称。 因此,您可以将多个计时器附加到一个对象并停止其中任何一个。
eval 函数(及其相关函数 Function、setTimeout 和 setInterval)提供对 JavaScript 编译器的访问。 有时这是必要的,但在大多数情况下,它表明存在极其糟糕的编码。 eval 函数是 JavaScript 中最常被滥用的功能。
http://www.jslint.com/lint.html
Using the jQuery timer will also allow you to have a name associated with the timers that are attached to the object. So you could attach several timers to an object and stop any one of them.
The eval function (and its relatives, Function, setTimeout, and setInterval) provide access to the JavaScript compiler. This is sometimes necessary, but in most cases it indicates the presence of extremely bad coding. The eval function is the most misused feature of JavaScript.
http://www.jslint.com/lint.html
也许最简单的方法是使用计时器插件。 http://plugins.jquery.com/project/timers 然后调用类似的内容
Probably the easiest way is to use the timers plugin. http://plugins.jquery.com/project/timers and then call something like
我们可以直接使用
we can directly use