📅  最后修改于: 2020-10-27 06:23:35             🧑  作者: Mango
警报消息通常用于向最终用户显示诸如警告或确认消息之类的信息。使用警报消息插件,您可以向所有警报消息添加关闭功能。
如果要单独包含此插件功能,则需要alert.js 。另外,如《 Bootstrap插件概述》一章所述,您可以包含bootstrap.js或缩小的bootstrap.min.js 。
您可以通过以下两种方式启用警报消除功能-
$(".alert").alert()
以下示例通过数据属性演示了警报插件的使用。
×
Warning! There was a problem with your network connection.
这里没有选项。
以下是警报插件的一些有用方法-
Method | Description | Example |
---|---|---|
.alert() | This method wraps all alerts with close functionality. |
$('#identifier').alert(); |
Close Method .alert(‘close’) | To enable all alerts to be closed, add this method. |
$('#identifier').alert('close'); |
要使警报在关闭时可以发出动画,请确保它们已应用.fade和.in类。
以下示例演示了.alert()方法的使用-
Alert messages to demonstrate alert() method
×
Success! the result is successful.
×
Warning! There was a problem with your network connection.
以下示例演示了.alert(‘close’)方法的用法-
Alert messages to demonstrate alert('close') method
×
Success! the result is successful.
×
Warning! There was a problem with your network connection.
使用Try it编辑器尝试此代码。您可以看到关闭功能已应用于所有警报消息,即关闭任何警报消息,其余警报消息也会被关闭。
下表列出了可与警报插件一起使用的事件。此事件可用于挂接到警报函数。
Event | Description | Example |
---|---|---|
close.bs.alert | This event fires immediately when the close instance method is called. |
$('#myalert').bind('close.bs.alert', function () { // do something }) |
closed.bs.alert | This event is fired when the alert has been closed (will wait for CSS transitions to complete). |
$('#myalert').bind('closed.bs.alert', function () { // do something }) |
以下示例演示了警报插件事件-
×
Success! the result is successful.