ส่วนประกอบโทสต์เปรียบเสมือนกล่องแจ้งเตือนที่แสดงเพียงไม่กี่วินาทีเมื่อมีบางอย่างเกิดขึ้น (เช่น เมื่อผู้ใช้คลิกปุ่ม ส่งแบบฟอร์ม ฯลฯ)
Some text inside the toast body
หากต้องการสร้างขนมปังปิ้ง ให้ใช้คลาส .toast
และเพิ่ม .toast-header
และ .toast-body
อยู่ข้างใน
หมายเหตุ: โทสต์จะถูกซ่อนไว้ตามค่าเริ่มต้น ใช้คลาส .show
หากคุณต้องการแสดง หากต้องการปิด ให้ใช้ <button> องค์ประกอบและเพิ่ม data-bs-dismiss="toast"
:
<div class="toast show">
<div class="toast-header">
Toast Header
<button type="button" class="btn-close" data-bs-dismiss="toast"></button>
</div>
<div class="toast-body">
Some text inside the toast body
</div>
</div>
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
<h3>Toast Example</h3>
<p>A toast is like an alert box that is only shown for a couple of seconds when something happens (i.e. when a user clicks on a button, submits a form, etc.).</p>
<p>In this example, we use the .show class to always show the toast by default. You can close it by clicking on the close (x) icon inside the toast header.</p>
<div class="toast show">
<div class="toast-header">
<strong class="me-auto">Toast Header</strong>
<button type="button" class="btn-close" data-bs-dismiss="toast"></button>
</div>
<div class="toast-body">
<p>Some text inside the toast body</p>
</div>
</div>
</div>
</body>
</html>
หากต้องการแสดงโทสต์ด้วยการคลิกปุ่ม คุณต้องเริ่มต้นด้วย JavaScript: เลือกองค์ประกอบที่ระบุและเรียกใช้เมธอด toast()
รหัสต่อไปนี้จะแสดง "ขนมปังปิ้ง" ทั้งหมดในเอกสารเมื่อคุณคลิกที่ปุ่ม:
<script>
document.getElementById("toastbtn").onclick = function() {
var toastElList = [].slice.call(document.querySelectorAll('.toast'))
var toastList = toastElList.map(function(toastEl) {
return new bootstrap.Toast(toastEl)
})
toastList.forEach(toast => toast.show())
}
</script>
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container mt-3">
<h3>Toast Example</h3>
<p>In this example, we use a button to show the toast message.</p>
<button type="button" class="btn btn-primary" id="toastbtn">Show Toast</button>
<div class="toast">
<div class="toast-header">
<strong class="me-auto">Toast Header</strong>
<button type="button" class="btn-close" data-bs-dismiss="toast"></button>
</div>
<div class="toast-body">
<p>Some text inside the toast body</p>
</div>
</div>
</div>
<script>
document.getElementById("toastbtn").onclick = function() {
var toastElList = [].slice.call(document.querySelectorAll('.toast'))
var toastList = toastElList.map(function(toastEl) {
return new bootstrap.Toast(toastEl)
})
toastList.forEach(toast => toast.show())
}
</script>
</body>
</html>