假设我有以下代码:
<div class="alert alert-info fade in" id="bsalert">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Info!</strong> This alert box could indicate a neutral informative or action
</div>
如何使用jQuery手动激活它?
答案 0 :(得分:8)
Bootstrap使用in
和out
类来查看可见性。你只需要切换这些类。此外,如果您希望在取消后保持警报,则可以向close.bs.alert
事件添加返回false。
function toggleAlert(){
$(".alert").toggleClass('in out');
return false; // Keep close.bs.alert event from removing from DOM
}
$("#btn").on("click", toggleAlert);
$('#bsalert').on('close.bs.alert', toggleAlert)
<link rel="stylesheet" type="text/css" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<button id="btn">Toggle</button>
<div class="alert alert-info fade out" id="bsalert">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Info!</strong> This alert box could indicate a neutral informative or action
</div>
<div>
content