[英]Do function before confirm jquery
here is my jquery code 这是我的jQuery代码
$('.input').keyup(function(event){
update_text(); // $('div').html('blahblah');
})
.blur(function(event){
$(this).keyup();
if(confirm('Are you sure?')){
// do somthing...
}
});
my problem is when input blur, confirm box should be show after doing update_text(); 我的问题是输入模糊时,请在执行update_text()之后显示确认框;
but update_text() seems run after confirm... 但是update_text()似乎在确认后运行...
How to make it do update_text() first, then show confirm dialog? 如何使其首先执行update_text(),然后显示确认对话框?
This might be too simple, but it seems like it fixes the problem. 这可能太简单了,但似乎可以解决问题。
$('.input').keyup(function(event){
update_text(); // $('div').html('blahblah');
})
$('.input').blur(function(event){
update_text();
if(confirm('Are you sure?')){
// do somthing...
}
});
Actually your method does run before confirm()
, the issue here is that your browser repaints the DOM asynchronously even though the method .html()
is in itself synchronous. 实际上,您的方法确实在
confirm()
之前运行,这里的问题是,即使方法.html()
本身是同步的,您的浏览器也会异步重绘DOM。 So while it doesn't appear to run first visually it indeed is. 因此,虽然它似乎没有在视觉上首先运行,但确实是。
For example a more simple version will have the same functionality, which can vary by browser: 例如,一个更简单的版本将具有相同的功能,具体取决于浏览器:
$("div").html("foo"); confirm('Are you sure?');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div></div>
One way to get around this is to force the DOM to repaint rather than letting the browser do it. 解决此问题的一种方法是强制DOM重新绘制,而不是让浏览器执行。 For example:
例如:
$("div").html("foo"); $("div").hide().show(function(){ confirm('Are you sure?'); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div></div>
update_text()
happens on keyup
event, which is triggered before the input is blurred, so if set up correctly it actually happens each time the user inputs another character. update_text()
发生在keyup
事件上,该事件在输入模糊之前触发,因此如果设置正确,则实际上每次用户输入另一个字符时都会发生。
$('.input').keyup(function(event){ update_text(); // $('div').html('blahblah'); }) .blur(function(event){ $(this).keyup(); if(confirm('Are you sure?')){ // do somthing... } }); update_text = function() { text = $('.input').val(); $('#text').text(text); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class='input'/> <div id='text'>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.