[英]how to execute some code before alert appears
I need to hide a div by clicking on it and AFTER the div is hidden - an alert should appears. 我需要通过单击它来隐藏div,并且在div隐藏之后-应该会出现警报。
Problem - the alert appears BEFORE the div is hidden. 问题-隐藏div之前会出现警报。
$('.lorem').on('click', function(){ $(this).hide(); alert('LOREM IS HIDDEN'); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class='lorem'>LOREM</div>
JQuery's .hide
offers a callback for when the element has disappeared. jQuery的
.hide
提供了元素消失时的回调 。
$('.lorem').on('click', function(){ $(this).hide(function() { alert('LOREM IS HIDDEN'); }); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class='lorem'>LOREM</div>
1.Using setTimeout will give you result you want 1.使用setTimeout会给你想要的结果
$('.lorem').on('click', function(){
$(this).hide();
setTimeout(function(){alert('LOREM IS HIDDEN')} , 0);
})
2.You can also use callback as second parameter of hide function https://api.jquery.com/hide/ 2.您也可以使用callback作为隐藏函数https://api.jquery.com/hide/的第二个参数
$('.lorem').on('click', function(){
$(this).hide(function() {
alert('LOREM IS HIDDEN'););
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.