如何在Bootstrap中创建一个基本的Modal组件
模板是JavaScript弹出式窗口,帮助我们在网站中提供非常有用的内容,如在网站上显示保存、删除、下载或关闭确认。Bootstrap modals是轻量级和多用途的JavaScript组件。它们也是可定制和可响应的组件。在这篇文章中,我们将学习如何使用bootstrap框架创建一个基本的模态组件。
为此,我们首先要在我们的HTML文件中导入以下bootstrap CDN。
<!– Bootstrap CSS –>
<link href=”https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css” rel=”stylesheet” integrity=”sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU” crossorigin=”anonymous”>
<!– Bootstrap JS –>
<script src=”https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.bundle.min.js” integrity=”sha384-/bQdsTh/da6pkI1MST/rWKFNjaCP5gBSY4sEBT38Q/9RBh9AH40zEOg7Hlq2THRZ” crossorigin=”anonymous”></script>
例子:在这个例子中,我们将看到如何使用Bootstrap模态组件创建一个基本模态。
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href=
"https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css"
rel="stylesheet" integrity=
"sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU"
crossorigin="anonymous">
<script src=
"https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.bundle.min.js"
integrity=
"sha384-/bQdsTh/da6pkI1MST/rWKFNjaCP5gBSY4sEBT38Q/9RBh9AH40zEOg7Hlq2THRZ"
crossorigin="anonymous">
</script>
</head>
<body>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary"
data-bs-toggle="modal"
data-bs-target="#exampleModal"
style="margin: 2em;">
Demo Modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal"
tabindex="-1" aria-labelledby="exampleModalLabel"
aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"
id="exampleModalLabel">
This is sample title.
</h5>
<button type="button" class="btn-close"
data-bs-dismiss="modal"
aria-label="Close">
</button>
</div>
<div class="modal-body">
This is the body of the modal.
</div>
<div class="modal-footer">
<button type="button"
class="btn btn-secondary"
data-bs-dismiss="modal">
Close
</button>
<button type="button"
class="btn btn-primary">
Save
</button>
</div>
</div>
</div>
</div>
</body>
</html>
输出: