如何使用 Twitter Bootstrap 自动关闭警报

编程入门 行业动态 更新时间:2024-10-11 05:21:06
本文介绍了如何使用 Twitter Bootstrap 自动关闭警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 twitter 的 bootstrap CSS 框架(这太棒了).对于给用户的一些消息,我使用警报 Javascript JS 和 CSS 来显示它们.

有兴趣的可以在这里找到:getbootstrap/javascript/#alerts>

我的问题是这样的;在向用户显示警报后,我希望它在一段时间后消失.根据 twitter 的文档和我浏览过的代码,它看起来没有包含在内:

  • 我的第一个问题是要求确认这确实没有融入 Bootstrap
  • 其次,我如何才能实现这种行为?

解决方案

调用 window.setTimeout(function, delay) 将允许您完成此操作.这是一个示例,它会在显示后 2 秒(或 2000 毫秒)自动关闭警报.

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

如果你想把它包装在一个漂亮的函数中,你可以这样做.

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

然后你可以像这样使用它......

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

我确信有更优雅的方法来实现这一点.

I'm using twitter's bootstrap CSS framework (which is fantastic). For some messages to users I am displaying them using the alerts Javascript JS and CSS.

For those interested, it can be found here: getbootstrap/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?

解决方案

Calling window.setTimeout(function, delay) will allow you to accomplish this. Here's an example that will automatically close the alert 2 seconds (or 2000 milliseconds) after it is displayed.

$(".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.

更多推荐

如何使用 Twitter Bootstrap 自动关闭警报

本文发布于:2023-11-27 18:57:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639210.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   如何使用   自动关闭   Bootstrap   Twitter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!