Bootstrap警报附加和淡入/淡出

时间:2018-06-02 09:31:36

标签: javascript jquery html css bootstrap-4

我想动态添加引导警报,效果为fadeInfadeOut效果关闭,我试过:

function alert(title, text, type) {
  var html = $("<div class='alert alert-dismissible hide fade " + type + "'><strong>" + title + "</strong> " + text + "<a href='#' class='close float-close' data-dismiss='alert' aria-label='close'>×</a></div>");
  $('body').append(html);
  html.addClass('in show');
}


$('#EMail').click(function() {
  alert('Error!', 'Your Email is not valid', 'alert-danger');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
<input type="text" id="EMail" />

使用此功能,成功关闭工作但附加fadeIn效果不起作用。如何在fadeIn效果的文档中附加Bootstrap警报?

请注意,我不需要jQuery fadeInfadeOut我只想使用标准引导程序class执行此操作fade + inout

1 个答案:

答案 0 :(得分:1)

使用setTimeout

更新了函数

function alert(title, text, type) {
  var html = $("<div class='alert alert-dismissible hide fade in " + type + "'><strong>" + title + "</strong> " + text + "<a href='#' class='close float-close' data-dismiss='alert' aria-label='close'>×</a></div>");
  $('body').append(html);
  setTimeout(function() {
 html.addClass('show');
  },0);
}


$('#EMail').click(function() {
  alert('Error!', 'Your Email is not valid', 'alert-danger');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
<input type="text" id="EMail" />