我正在使用twitter的 bootstrap 式CSS框架(非常棒).对于发送给用户的一些消息,我使用Javascript JS和CSS来显示它们.

对于那些感兴趣的人,可以在这里找到它:http://getbootstrap.com/javascript/#alerts

My issue is this; after I've displayed an alert to a user I'd like it to just go away after some time interval. Based on twitter's docs and the code I've looked through it looks like this is not baked in:

  • My first question is a request for confirmation that this is indeed NOT baked into Bootstrap
  • Secondly, how can I achieve this behavior?

推荐答案

拨打window.setTimeout(function, delay)可以让你实现这一点.以下示例将在alert 显示2秒(或2000毫秒)后自动关闭alert .

$(".alert-message").alert();
window.setTimeout(function() { $(".alert-message").alert('close'); }, 2000);

If you want to wrap it in a nifty function you could do this.

function createAutoClosingAlert(selector, delay) {
   var alert = $(selector).alert();
   window.setTimeout(function() { alert.alert('close') }, delay);
}

Then you could use it like so...

createAutoClosingAlert(".alert-message", 2000);

I am certain there are more elegant ways to accomplish this.

Jquery相关问答推荐

TinyMCE不保存区块报价

Bootstrap 3模态框和video.js视频未正确调整大小

用 jQuery/JavaScript 检测数字或字母?

从文本区域获取值

jQuery / Ajax - $.ajax() 将参数传递给回调 - 使用好的模式?

jQuery:获取所选单选按钮的父 tr

以ajax方式在rails 3中提交表单(使用jQuery)

动态设置 iframe src

jQuery 判断 是否存在并且有一个值

使用 jQuery DataTables 时禁用最后一列的排序

将参数附加到 URL 而不刷新

如何在 JavaScript 或 jQuery 中规范化 HTML?

使用jquery设置di​​v标签的值

使用 jQuery DataTables 时禁用第一列的自动排序

jquery在加载时获取iframe内容的高度

使用 jQuery 在 Select 列表中隐藏选项

将十六进制转换为 RGBA

D3 和​​ jQuery 有什么区别?

'touchstart' 事件是否有与点击事件相同的 e.PageX 位置?

jQuery - 不可见时获取元素的宽度(显示:无)