Bootstrap 5 解决方案 --> 使用 .NET Core 5.0 测试
引导警报 HTML 代码(这些代码存储在 Razor 视图组件中)
<!-------------------------------->
<!-- INFORMATION Bootstrap Alert-->
<!-------------------------------->
<div class="alert alert-info alert-dismissible fade show bg-info" role="alert" id="bootstrapInformationAlertDiv" style="display:none;">
<div class="custom-alert-div-information text-light font-weight-bold text-dark" id="bootstrapInformationAlertMessageDiv"> </div>
<button type="button" class="btn-close" aria-label="Close" id="bootstrapInformationAlertBtn"></button>
</div>
<!---------------------------->
<!-- SUCCESS Bootstrap Alert-->
<!---------------------------->
<div class="alert alert-success alert-dismissible fade show bg-success" role="alert" id="bootstrapSuccessAlertDiv" style="display:none;">
<div class="custom-alert-div-success text-light font-weight-bold" id="bootstrapSuccessAlertMessageDiv"> </div>
<button type="button" class="btn-close" aria-label="Close" id="bootstrapSuccessAlertBtn"></button>
</div>
<!---------------------------->
<!-- WARNING Bootstrap Alert-->
<!---------------------------->
<div class="alert alert-warning alert-dismissible fade show bg-warning" role="alert" id="bootstrapWarningAlertDiv" style="display:none;">
<div class="custom-alert-div-warning text-black font-weight-bold" id="bootstrapWarningAlertMessageDiv"> </div>
<button type="button" class="btn-close" aria-label="Close" id="bootstrapWarningAlertBtn"></button>
</div>
<!--------------------------->
<!-- DANGER Bootstrap Alert-->
<!--------------------------->
<div class="alert alert-danger alert-dismissible fade show bg-danger" role="alert" id="bootstrapDangerAlertDiv" style="display:none;">
<div class="custom-alert-div-danger text-light font-weight-bold" id="bootstrapDangerAlertMessageDiv"> </div>
<button type="button" class="btn-close" aria-label="Close" id="bootstrapDangerAlertBtn"></button>
</div>
上面的视图组件添加在每个网页的顶部:
<!-- Bootsrap Alert View Component -->
<div>
@await Component.InvokeAsync("BootstrapAlert")
</div>
然后我在我的项目中添加了一个小的 JS 文件 (bootstrapAlert.js):
$("#bootstrapInformationAlertBtn").click(function () {
event.preventDefault();
$(this).parent().hide();
});
$("#bootstrapSuccessAlertBtn").click(function () {
event.preventDefault();
$(this).parent().hide();
});
$("#bootstrapWarningAlertBtn").click(function () {
event.preventDefault();
$(this).parent().hide();
});
$("#bootstrapDangerAlertBtn").click(function () {
event.preventDefault();
$(this).parent().hide();
});
在我的主 Layout.cshtml 文件中,我将脚本引用添加到上面的 js 文件中,以便它自动加载到我网站中的每个页面:
<!-- Load the js script file on each page for hiding the bootstrap alert when closed -->
<script src="~/js/bootstrap-alert-custom-scripts/bootstrapAlert.js"></script>
当调用引导警报以从我的任何 javascript 函数中打开时,我使用以下代码:
window.scrollTo(0, 0); // Scroll to the top of the page.
document.getElementById("bootstrapWarningAlertMessageDiv").innerHTML = 'Write your custom message here for the current issue / notification!';
$("#bootstrapWarningAlertDiv").show();
setTimeout(function () { $("#bootstrapWarningAlertDiv").hide(); }, 8000);
窗口滚动到命令是一个不错的小触摸,因为用户可能会向下滚动页面,如果您总是在同一个位置(即页面顶部)添加警报,那么他们不会打开时一定会看到它。