View on GitHub
警告框
为典型用户操作提供上下文反馈消息,并提供少量可用且灵活的警报消息。
示例
警报可用于任何长度的文本,以及一个可选的关闭按钮。要获得正确的样式,请使用八个 必需 的上下文类之一(例如, .alert-success
)。对于内联 解除,请使用 alerts jQuery插件。
A simple primary alert—check it out!
A simple secondary alert—check it out!
A simple success alert—check it out!
A simple danger alert—check it out!
A simple warning alert—check it out!
A simple info alert—check it out!
A simple light alert—check it out!
A simple dark alert—check it out!
<div class="alert alert-primary" role="alert">
A simple primary alert—check it out!
</div>
<div class="alert alert-secondary" role="alert">
A simple secondary alert—check it out!
</div>
<div class="alert alert-success" role="alert">
A simple success alert—check it out!
</div>
<div class="alert alert-danger" role="alert">
A simple danger alert—check it out!
</div>
<div class="alert alert-warning" role="alert">
A simple warning alert—check it out!
</div>
<div class="alert alert-info" role="alert">
A simple info alert—check it out!
</div>
<div class="alert alert-light" role="alert">
A simple light alert—check it out!
</div>
<div class="alert alert-dark" role="alert">
A simple dark alert—check it out!
</div>
向辅助技术传达意义
使用颜色来增加意义只会提供一种视觉指示,而不会传达给辅助技术(如屏幕阅读器)的用户。确保由颜色表示的信息在内容本身(例如可见文本)中是明显的,或者通过其他方式包含,例如用.sr-only
类隐藏的其他文本。
链接颜色
使用 .alert-link
实用程序类可以在任何警报中快速提供匹配的彩色链接。
A simple primary alert with an example link. Give it a click if you like.
A simple secondary alert with an example link. Give it a click if you like.
A simple success alert with an example link. Give it a click if you like.
A simple danger alert with an example link. Give it a click if you like.
A simple warning alert with an example link. Give it a click if you like.
A simple info alert with an example link. Give it a click if you like.
A simple light alert with an example link. Give it a click if you like.
A simple dark alert with an example link. Give it a click if you like.
<div class="alert alert-primary" role="alert">
A simple primary alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-secondary" role="alert">
A simple secondary alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-success" role="alert">
A simple success alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-danger" role="alert">
A simple danger alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-warning" role="alert">
A simple warning alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-info" role="alert">
A simple info alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-light" role="alert">
A simple light alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-dark" role="alert">
A simple dark alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
附加内容
警报还可以包含其他HTML元素,如标题、段落和分隔符。
Well done!
Aww yeah, you successfully read this important alert message. This example text is going to run a bit longer so that you can see how spacing within an alert works with this kind of content.
Whenever you need to, be sure to use margin utilities to keep things nice and tidy.
<div class="alert alert-success" role="alert">
<h4 class="alert-heading">Well done!</h4>
<p>Aww yeah, you successfully read this important alert message. This example text is going to run a bit longer so that you can see how spacing within an alert works with this kind of content.</p>
<hr>
<p class="mb-0">Whenever you need to, be sure to use margin utilities to keep things nice and tidy.</p>
</div>
解除警告
使用alert JavaScript插件,可以关闭任何内联警报。方法如下:
- 确保已加载警报插件或已编译的 Bootstrap JavaScript.
- 如果您是从源代码构建我们的JavaScript,它需要
util.js
。编译版本包括这个。 - 添加一个dismiss按钮和
.alert-dismissible
类,该类在警报的右侧添加额外的填充,并放置.close
按钮。 - 在dismiss按钮上,添加
data-dismiss="alert"
属性,该属性触发JavaScript功能。一定要使用<button>
元素在所有设备上进行正确的操作。 - 要在解除警报时设置警报动画,请确保添加
.fade
和.show
类。
您可以在现场演示中看到这一点:
Holy guacamole! You should check in on some of those fields below.
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<strong>Holy guacamole!</strong> You should check in on some of those fields below.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
JavaScript 行为
触发器
启用通过JavaScript取消警报:
$('.alert').alert()
或者在警报中的按钮上显示 data
属性,如上所示:
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
请注意,关闭警报会将其从DOM中删除。
方法
方法 | 描述 |
---|---|
$().alert() |
使警报侦听具有 data-dismiss="alert" 属性的子元素上的单击事件。(使用数据api的自动初始化时不需要。) |
$().alert('close') |
通过从DOM中删除警报来关闭警报。如果元素上存在.fade 和 .show 类,则警报将在删除之前淡出。 |
$().alert('dispose') |
销毁元素的警报。 |
$('.alert').alert('close')
事件
Bootstrap的警报插件公开了一些事件,用于连接警报功能。
事件 | 描述 |
---|---|
close.bs.alert |
调用 close 实例方法时,此事件立即激发。 |
closed.bs.alert |
此事件在警报关闭时触发(将等待CSS转换完成)。 |
$('#myAlert').on('closed.bs.alert', function () {
// do something...
})