Bootstrap 警报自动关闭

编程入门 行业动态 更新时间:2024-10-27 12:34:05
本文介绍了Bootstrap 警报自动关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要在单击添加到心愿单"按钮时调用提醒,并且应该在 2 秒内消失提醒.这就是我尝试的方式,但是警报一出现就会立即消失.不确定,错误在哪里.. 任何人都可以帮助我吗?

JS 脚本

$(document).ready (function(){$("#success-alert").hide();$("#myWish").click(function showAlert() {$("#success-alert").alert();window.setTimeout(function () {$("#success-alert").alert('close');}, 2000);});});

HTML 代码:

<a id="myWish" href="" class="btn btn-mini">添加到心愿单</a><a href="#" class="btn btn-mini">购买

警报框:

<button type="button" class="close" data-dismiss="alert">x</button><strong>成功!</strong>产品已添加到您的愿望清单.

解决方案

为了平滑上滑:

$("#success-alert").fadeTo(2000, 500).slideUp(500, function(){$("#success-alert").slideUp(500);});

$(document).ready(function() {$("#success-alert").hide();$("#myWish").click(function showAlert() {$("#success-alert").fadeTo(2000, 500).slideUp(500, function() {$("#success-alert").slideUp(500);});});});

<link rel="stylesheet" href="stackpath.bootstrapcdn/bootstrap/4.3.1/css/bootstrap.min.css"integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"><div class="product-options"><a id="myWish" href="javascript:;"class="btn btn-mini"> 添加到愿望清单 </a><a href="" class="btn btn-mini">购买

<div class="alert alert-success" id="success-alert"><button type="button" class="close" data-dismiss="alert">x</button><strong>成功!产品已添加到您的愿望清单.

<script src="cdnjs.cloudflare/ajax/libs/jquery/3.3.1/jquery.min.js"></script><script src="stackpath.bootstrapcdn/bootstrap/4.3.1/js/bootstrap.min.js"integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+jRMB"cross/script>

My need is to call alert when I click on Add to Wishlist button and should disappear the alert in 2 secs. This is how I tried, but the alert is disappearing instantly as soon as it is appearing. Not sure, where the bug is.. Can anyone help me out?

JS Script

$(document).ready (function(){ $("#success-alert").hide(); $("#myWish").click(function showAlert() { $("#success-alert").alert(); window.setTimeout(function () { $("#success-alert").alert('close'); }, 2000); }); });

HTML Code:

<div class="product-options"> <a id="myWish" href="" class="btn btn-mini">Add to Wishlist </a> <a href="#" class="btn btn-mini"> Purchase </a> </div>

Alert Box:

<div class="alert alert-success" id="success-alert"> <button type="button" class="close" data-dismiss="alert">x</button> <strong>Success!</strong> Product have added to your wishlist. </div>

解决方案

For a smooth slideup:

$("#success-alert").fadeTo(2000, 500).slideUp(500, function(){ $("#success-alert").slideUp(500); });

$(document).ready(function() { $("#success-alert").hide(); $("#myWish").click(function showAlert() { $("#success-alert").fadeTo(2000, 500).slideUp(500, function() { $("#success-alert").slideUp(500); }); }); });

<link rel="stylesheet" href="stackpath.bootstrapcdn/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <div class="product-options"> <a id="myWish" href="javascript:;" class="btn btn-mini">Add to Wishlist </a> <a href="" class="btn btn-mini"> Purchase </a> </div> <div class="alert alert-success" id="success-alert"> <button type="button" class="close" data-dismiss="alert">x</button> <strong>Success! </strong> Product have added to your wishlist. </div> <script src="cdnjs.cloudflare/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="stackpath.bootstrapcdn/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

更多推荐

Bootstrap 警报自动关闭

本文发布于:2023-11-27 16:50:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638819.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   自动关闭   Bootstrap

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!