警告框

使用少量可用且灵活的警告框消息,为典型用户操作提供上下文反馈消息。

JS Optional
Layer: components

默认警告框

警告框是 Flex 容器,可以包含任意长度的文本,以及可选的关闭按钮。它们设计为与主题颜色一起使用,但在未应用主题颜色时具有中性回退颜色。若要内联关闭,请使用警告框 JavaScript 插件。也支持自定义内容,但需要额外的工具类或标记。

HTML
<div class="alert" role="alert">
  <p>A simple alert—check it out!</p>
</div>

变体

使用我们任意一个变体主题类,实现颜色模式自适应的上下文样式。

HTML
<div class="alert theme-primary" role="alert">
  <p>A simple primary alert—check it out!</p>
</div>
<div class="alert theme-accent" role="alert">
  <p>A simple accent alert—check it out!</p>
</div>
<div class="alert theme-success" role="alert">
  <p>A simple success alert—check it out!</p>
</div>
<div class="alert theme-danger" role="alert">
  <p>A simple danger alert—check it out!</p>
</div>
<div class="alert theme-warning" role="alert">
  <p>A simple warning alert—check it out!</p>
</div>
<div class="alert theme-info" role="alert">
  <p>A simple info alert—check it out!</p>
</div>
<div class="alert theme-inverse" role="alert">
  <p>A simple inverse alert—check it out!</p>
</div>
<div class="alert theme-secondary" role="alert">
  <p>A simple secondary alert—check it out!</p>
</div>
无障碍提示:使用颜色传达含义

仅使用颜色来增添含义只能提供视觉上的提示,无法传达给屏幕阅读器等辅助技术的用户。请确保含义本身从内容中就能显而易见(例如,具有_足够_颜色对比度的可见文本),或通过其他方式包含,例如使用 .visually-hidden 类隐藏的附加文本。

实时示例

点击下面的按钮以显示一个警告框(初始时通过内联样式隐藏),然后使用内置关闭按钮将其关闭(并销毁)。

HTML
<div id="liveAlertPlaceholder"></div>
<button type="button" class="btn-solid theme-primary flex-grow-0" id="liveAlertBtn">Show live alert</button>

我们使用以下 JavaScript 来触发实时警告框演示:

const alertPlaceholder = document.getElementById('liveAlertPlaceholder')
const appendAlert = (message, type) => {
  const wrapper = document.createElement('div')
  wrapper.innerHTML = [
    `<div class="alert theme-${type}" role="alert">`,
    `   <p>${message}</p>`,
    '   <button type="button" class="btn-close ms-auto" data-bs-dismiss="alert" aria-label="Close"></button>',
    '</div>'
  ].join('')

  alertPlaceholder.append(wrapper)
}

const alertTrigger = document.getElementById('liveAlertBtn')
if (alertTrigger) {
  alertTrigger.addEventListener('click', () => {
    appendAlert('Nice, you triggered this alert message!', 'success')
  })
}

链接颜色

使用 .alert-link 工具类即可在任何警告框内快速提供匹配颜色的链接。

HTML
<div class="alert theme-primary" role="alert">
  <p>A simple primary alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.</p>
</div>
<div class="alert theme-accent" role="alert">
  <p>A simple accent alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.</p>
</div>
<div class="alert theme-success" role="alert">
  <p>A simple success alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.</p>
</div>
<div class="alert theme-danger" role="alert">
  <p>A simple danger alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.</p>
</div>
<div class="alert theme-warning" role="alert">
  <p>A simple warning alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.</p>
</div>
<div class="alert theme-info" role="alert">
  <p>A simple info alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.</p>
</div>
<div class="alert theme-inverse" role="alert">
  <p>A simple inverse alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.</p>
</div>
<div class="alert theme-secondary" role="alert">
  <p>A simple secondary alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.</p>
</div>

带图标

同样,你可以使用 Flex 布局工具类 和 Bootstrap Icons 创建带图标的警告框。根据你的图标和内容,你可能需要添加更多工具类或自定义样式。

HTML
<div class="alert theme-primary align-items-center" role="alert">
  <svg xmlns="http://www.w3.org/2000/svg" class="bi flex-shrink-0" viewBox="0 0 16 16" role="img" aria-label="Warning:">
    <path d="M8.982 1.566a1.13 1.13 0 0 0-1.96 0L.165 13.233c-.457.778.091 1.767.98 1.767h13.713c.889 0 1.438-.99.98-1.767L8.982 1.566zM8 5c.535 0 .954.462.9.995l-.35 3.507a.552.552 0 0 1-1.1 0L7.1 5.995A.905.905 0 0 1 8 5zm.002 6a1 1 0 1 1 0 2 1 1 0 0 1 0-2z"/>
  </svg>
  <p>An example alert with an icon</p>
</div>

关闭

使用警告框 JavaScript 插件,可以内联关闭任何警告框。方法如下:

  • 确保你已加载警告框插件,或已编译的 Bootstrap JavaScript。
  • 添加一个关闭按钮,并使用 Flex 布局工具类(例如 ms-auto)将其定位在警告框内。
  • 在关闭按钮上,添加 data-bs-dismiss="alert" 属性,它会触发 JavaScript 功能。请务必搭配使用 <button> 元素,以便在所有设备上都具有正确行为。

警告框在移除之前会淡出。插件会在过渡期间添加 .hiding,因此你的标记中无需额外的类。添加 .alert-instant 可立即移除警告框。有关调节淡出效果的 tokens,请参阅过渡。

你可以通过实时演示查看实际操作:

HTML
<div class="alert theme-warning" role="alert">
  <p>Something’s wrong, check the fields below and try again.</p>
  <button type="button" class="btn-close ms-auto" data-bs-dismiss="alert" aria-label="Close"></button>
</div>

关闭警告框时,该元素会从页面结构中完全移除。如果键盘用户使用关闭按钮关闭警告框,他们的焦点会突然丢失,并视浏览器而定重置到页面/文档的开头。因此,我们建议包含额外的 JavaScript,监听 closed.bs.alert 事件并以编程方式将 focus() 设置到页面中最合适的位置。如果你打算将焦点移动到通常不接收焦点的非交互元素,请确保为该元素添加 tabindex="-1"。

额外内容

对于简单的垂直布局,为警告框添加 flex-column 类通常就足够了。

HTML
<div class="alert theme-success flex-column align-items-stretch" role="alert">
  <h4 class="alert-heading mb-2">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 class="my-3" />
  <p>Whenever you need to, be sure to use margin utilities to keep things nice and tidy.</p>
</div>

更复杂的布局可能需要在警告框内添加额外的 HTML——例如混合图标和关闭按钮时。

HTML
<div class="alert theme-warning" role="alert">
  <div class="pe-3">
    <h5 class="alert-heading mb-2">More complex alert</h5>
    <p class="fs-sm mb-0">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>
  </div>
  <button type="button" class="btn-close ms-auto" data-bs-dismiss="alert" aria-label="Close"></button>
</div>

CSS

变量

Alerts use local CSS variables on .alert for real-time customization. Values for the CSS variables are generated from Sass maps unique to each component and applied to the aforementioned class.

// stylelint-disable-next-line scss/dollar-variable-default
$alert-tokens: defaults(
  (
    --alert-gap: var(--spacer-3),
    --alert-bg: var(--bg-1),
    --alert-padding-x: var(--spacer),
    --alert-padding-y: var(--spacer),
    --alert-color: inherit,
    --alert-border-width: var(--border-width),
    --alert-border-color: var(--border-color),
    --alert-border-radius: var(--radius-7),
    --alert-link-color: inherit,
    --alert-transition-property: "opacity",
    --alert-transition-duration: .15s,
    --alert-transition-timing: linear,
    --hr-border-color: var(--theme-border, var(--alert-border-color)),
  ),
  $alert-tokens
);

Sass 循环

用于生成修饰符类的循环,并覆盖 CSS 变量。

JavaScript 行为

初始化

将元素初始化为警告框

JavaScript
          const alertList = document.querySelectorAll('.alert')
const alerts = [...alertList].map(element => new bootstrap.Alert(element))
        

如果仅仅是为了关闭警告框,则无需通过 JS API 手动初始化组件。通过使用 data-bs-dismiss="alert",组件将自动初始化并正确关闭。

有关更多详细信息,请参阅触发器部分。

通过数据属性

属性描述
data-bs-dismiss="alert"在关闭控件上,激活时关闭(并移除)警告框。

触发器

Dismissal can be achieved with the data-bs-dismiss attribute on a button within the alert as demonstrated below:

HTML
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>

or on a button outside the alert using the additional data-bs-target as demonstrated below:

HTML
<button type="button" class="btn-close" data-bs-dismiss="alert" data-bs-target="#my-alert" aria-label="Close"></button>

请注意,关闭警告框会将其从 DOM 中移除。

依赖

The alert plugin requires the following JavaScript files if you’re building Bootstrap’s JS from source:

FileDescription
js/src/alert.tsMain alert component
js/src/base-component.tsBase component class
js/src/dom/data.tsElement data store
js/src/dom/event-handler.tsEvent handling utilities
js/src/dom/manipulator.tsData attribute manipulation
js/src/dom/selector-engine.tsDOM selector utilities
js/src/util/component-functions.tsShared component helpers
js/src/util/config.tsConfiguration base class
js/src/util/index.tsCore utility functions

方法

用于显示或隐藏组件的方法会返回一个 promise。 该 promise 在过渡结束时 resolve,因此 await 会在与 shown.bs.* 或 hidden.bs.* 监听器触发的同一时刻继续执行。这些方法仍会立即返回,绝不会阻塞调用方。对正处于过渡中的组件进行调用不会产生任何效果,并会立即 resolve。在我们的 JavaScript 文档中了解更多。

你可以使用警告框构造函数创建警告框实例,例如:

JavaScript
          const bsAlert = new bootstrap.Alert('#myAlert')
        

这会使警告框监听具有 data-bs-dismiss="alert" 属性的后代元素上的点击事件。(使用 data-api 自动初始化时无需这样做。)

方法描述
close通过从 DOM 中移除警告框来关闭它。警告框会先淡出,除非它具有 .alert-instant。返回一个 promise,在警告框被移除后 resolves(即在 closed.bs.alert 事件触发之后)。
dispose销毁元素的警告框。(移除 DOM 元素上存储的数据)
getInstance静态方法,允许你获取与 DOM 元素关联的警告框实例。例如:bootstrap.Alert.getInstance(alert)。
getOrCreateInstance静态方法,返回与 DOM 元素关联的警告框实例,如果尚未初始化则创建一个新实例。你可以这样使用:bootstrap.Alert.getOrCreateInstance(element)。

基本用法:

JavaScript
          const alert = bootstrap.Alert.getOrCreateInstance('#myAlert')
alert.close()
        

事件

Bootstrap 的警告框插件公开了几个事件,用于挂钩警告框功能。

事件描述
close.bs.alert当调用 close 实例方法时立即触发。
closed.bs.alert当警告框已关闭且 CSS 过渡已完成时触发。
JavaScript
          const myAlert = document.getElementById('myAlert')
myAlert.addEventListener('closed.bs.alert', event => {
  // do something, for instance, explicitly move focus to the most appropriate element,
  // so it doesn’t get lost/reset to the start of the page
  // document.getElementById('...').focus()
})