如何在触发警报之前制作HTML渲染? [英] How to make the HTML renders before the alert is triggered?
本文介绍了如何在触发警报之前制作HTML渲染?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问题实际上很简单.如何在警报显示之前更改div的内容?
The question is actually quite simple. How do I make the div's content change before the alert shows up?
JSFiddle:
https://jsfiddle.net/n2n5drL2/1/
HTML:
<div id="test">
Some Text
</div>
<button id="change">
change
</button>
JavaScript:
JavaScript:
$('#change').click(function(){
$('#test').text('new content');
alert('how to make this alert shows after the div changes its content?')
})
推荐答案
您需要推迟执行阻塞的 alert
,以便浏览器可以重新呈现更改后的DOM.您可以使用 setTimeout
和0ms超时:
You need to defer execution of the blocking alert
so the browser can re-render the changed DOM. You can do this with setTimeout
and a 0ms timeout:
$('#change').click(function(){
$('#test').text('new content');
setTimeout(function() {
alert('how to make this alert shows after the div changes its content?')
},0);
})
但是,值得注意的是, 警告
仅对
(可选)在等待用户确认消息时暂停.
Optionally, pause while waiting for the user to acknowledge the message.
因此,尽管我个人从未见过用户代理将其视为非阻塞性,但规范确实允许这样做.
So while I've personally never seen a user-agent treat it as non-blocking, the specification does allow for it.
这篇关于如何在触发警报之前制作HTML渲染?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文