نمونه ها
هشدارها برای هر طول متن و همچنین یک دکمه بسته اختیاری در دسترس هستند. برای استایل مناسب، از یکی از هشت مورد استفاده کنید ضروری کلاس های زمینه ای (به عنوان مثال، .alert-success). برای اخراج درون خطی، از پلاگین جاوا اسکریپت هشدار.
<div role="alert" class="alert alert-primary">A simple primary alert—check it out!</div>
<div role="alert" class="alert alert-secondary">A simple secondary alert—check it out!</div>
<div role="alert" class="alert alert-success">A simple success alert—check it out!</div>
<div role="alert" class="alert alert-danger">A simple danger alert—check it out!</div>
<div role="alert" class="alert alert-warning">A simple warning alert—check it out!</div>
<div role="alert" class="alert alert-info">A simple info alert—check it out!</div>
<div role="alert" class="alert alert-light">A simple light alert—check it out!</div>
<div role="alert" class="alert alert-dark">A simple dark alert—check it out!</div>
انتقال معنا به فناوری های کمکی
استفاده از رنگ برای افزودن معنی فقط یک نشانه بصری ارائه می دهد که به کاربران فناوری های کمکی - مانند صفحه خوان ها - منتقل نمی شود. اطمینان حاصل کنید که اطلاعات مشخص شده با رنگ یا از خود محتوا واضح است (مثلاً متن قابل مشاهده)، یا از طریق ابزارهای جایگزین، مانند متن اضافی پنهان شده با کلاس .visually-hidden.
رنگ لینک
استفاده از .alert-link کلاس ابزار برای ارائه سریع پیوندهای رنگی مطابق با هر هشدار.
<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 مانند سرفصل ها، پاراگراف ها و تقسیم کننده ها باشند.
آفرین!
اوه، بله، شما با موفقیت این پیام هشدار مهم را خواندید. این متن نمونه کمی طولانیتر اجرا میشود تا بتوانید نحوه عملکرد فاصله در یک هشدار با این نوع محتوا را ببینید..
هر زمان که نیاز داشتید، حتما از ابزارهای حاشیه استفاده کنید تا همه چیز را زیبا و مرتب نگه دارید.
<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-dismissibleکه بالشتک اضافی را در سمت راست هشدار اضافه می کند و دکمه بستن را در موقعیت قرار می دهد. - در دکمه بستن، را اضافه کنید
data-bs-dismiss="alert"ویژگی، که عملکرد جاوا اسکریپت را فعال می کند. حتما استفاده کنید<button>عنصر با آن برای رفتار مناسب در همه دستگاه ها. - برای متحرک کردن هشدارها هنگام رد کردن آنها، حتماً آن را اضافه کنید
.fadeو.showکلاس.
شما می توانید این را در عمل با یک نسخه ی نمایشی زنده ببینید:
<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="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
closed.bs.alert رویداد و به صورت برنامهریزی تنظیم میشود focus() به مناسب ترین مکان در صفحه. اگر میخواهید فوکوس را به عنصر غیرتعاملی که معمولاً فوکوس دریافت نمیکند منتقل کنید، مطمئن شوید tabindex="-1" به عنصر.
رفتار جاوا اسکریپت
محرک ها
رد کردن یک هشدار را از طریق جاوا اسکریپت فعال کنید:
var alertList = document.querySelectorAll('.alert')
alertList.forEach(function (alert) {
new bootstrap.Alert(alert)
})
یا با data ویژگی های روی یک دکمه within the alert، همانطور که در بالا نشان داده شد:
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
توجه داشته باشید که بستن یک هشدار آن را از DOM حذف می کند.
روش ها
برای مثال میتوانید با سازنده هشدار یک نمونه هشدار ایجاد کنید:
var myAlert = document.getElementById('myAlert')
var bsAlert = new bootstrap.Alert(myAlert)
این باعث میشود که یک هشدار به رویدادهای کلیک روی عناصر نسلی که دارای آن هستند گوش دهد data-bs-dismiss="alert" صفت. (در هنگام استفاده از data-api’s auto-initialization.)
| متد | توضیحات |
|---|---|
close
|
یک هشدار را با حذف آن از DOM می بندد. اگر .fade و .show کلاس ها روی عنصر وجود دارند، هشدار قبل از حذف محو می شود.
|
dispose
|
هشدار یک عنصر را از بین می برد. |
getInstance
|
روش ایستا که به شما امکان می دهد نمونه هشدار مربوط به یک عنصر DOM را دریافت کنید، می توانید از آن به این صورت استفاده کنید: bootstrap.Alert.getInstance(alert)
|
var alertNode = document.querySelector('.alert')
var alert = bootstrap.Alert.getInstance(alertNode)
alert.close()
رویدادها
افزونه هشدار بوت استرپ چند رویداد را برای اتصال به عملکرد هشدار در معرض نمایش می گذارد.
| رویداد | توضیحات |
|---|---|
close.bs.alert |
بلافاصله آتش می گیرد که close روش نمونه نامیده می شود.
|
closed.bs.alert |
زمانی که هشدار بسته شد و انتقال CSS تکمیل شد، فعال می شود. |
var myAlert = document.getElementById('myAlert')
myAlert.addEventListener('closed.bs.alert', function () {
// 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()
})