Bootstrap Alert Plugin

Alert messages are mostly used to display information such as warnings or confirmation messages to end users. With the Alert plugin, you can add a dismiss feature to all alert messages.

Usage

You can enable the dismissal functionality of alerts in the following two ways:

  • Via data attributes: To add the dismiss functionality via the Data API, simply add to the close buttondata-dismiss="alert", and the close functionality will be added to the alert automatically.
    <a class="close" data-dismiss="alert" href="../index.html" aria-hidden="true">
        &times;
    </a>
    
  • Via JavaScript: To add the dismiss functionality via JavaScript:
    $(".alert").alert()
    

Example

The following example demonstrates how to use the Alert plugin via data attributes.

Example

<div class="alert alert-warning"> <a href="#" class="close" data-dismiss="alert"> &times; </a> <strong>Warning!</strong>There is a problem with your network connection.</div>

Try it »

The result is as follows:

警告框(Alert)插件

Options

There are no options.

Methods

The following are some useful methods in the Alert plugin:

MethodDescriptionExample
.alert()This method makes all alert boxes have a close functionality.
$('#identifier').alert();
Close method .alert('close')Closes all alert boxes.
$('#identifier').alert('close');
To enable animation effects when closing, make sure to add the.fadeand.in class。

Example

The following example demonstrates the.alert()usage of the method:

Example

<h3>Alert plugin alert() method</h3> <div id="myAlert" class="alert alert-success"> <a href="#" class="close" data-dismiss="alert">&times;</a> <strong>Success!</strong>The result is successful.</div> <div id="myAlert2" class="alert alert-warning"> <a href="#" class="close" data-dismiss="alert">&times;</a> <strong>Warning!</strong>There is a problem with your network connection.</div> <script>
$(function(){ $(".close").click(function(){ $("#myAlert").alert(); $("#myAlert2").alert(); }); });
</script>

Try it »

The following example demonstrates the.alert('close')usage of the method:

Example

<h3>Alert plugin alert('close') method</h3> <div id="myAlert" class="alert alert-success"> <a href="#" class="close" data-dismiss="alert">&times;</a> <strong>Success!</strong>The result is successful.</div> <div id="myAlert2" class="alert alert-warning"> <a href="#" class="close" data-dismiss="alert">&times;</a> <strong>Warning!</strong>There is a problem with your network connection.</div> <script>
$(function(){ $(".close").click(function(){ $("#myAlert").alert('close'); $("#myAlert2").alert('close'); }); });
</script>

Try it »

You can see that all alert boxes have the close functionality applied, i.e., closing any alert will also close the remaining alerts.

Events

The following table lists the events used in the Alert plugin. These events can be used as hooks in functions.

EventDescriptionExample
close.bs.alertWhen thecloseinstance method is called, this event is triggered immediately.
$('#myalert').bind('close.bs.alert', function () {
  // 执行一些动作...
})
closed.bs.alertThis event is triggered when the alert is closed (it waits for the CSS transition to complete).
$('#myalert').bind('closed.bs.alert', function () {
    // 执行一些动作...
})

Example

The following example demonstrates the events of the Alert plugin:

Example

<div id="myAlert" class="alert alert-success"> <a href="#" class="close" data-dismiss="alert">&times;</a> <strong>Success!</strong>The result is successful.</div> <script>
$(function(){ $("#myAlert").bind('closed.bs.alert', function () { alert("The warning message box is closed."); }); });
</script>

Try it »

The result is as follows:

警告框(Alert)插件事件
Other extensions