我有以下 HTML 代码。当屏幕被“cover”DIV 覆盖时,单击按钮和其他位置。未点击按钮不会触发。稍后,当“封面”DIV 被隐藏时,点击按钮就会触发。
似乎存在时间问题。有人可以解释这种行为吗?
标记:
<div id="cover"></div>
<button onclick="alert('Button clicked')">Click me</button>
<div id="text"></div>
CSS:
#cover {
display: none;
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
z-index: 100;
background-color: white;
opacity: 0.8
}
Javascript:
function hide ()
{
document.getElementById ("cover").style.display = "none";
}
function show ()
{
document.getElementById ("cover").style.display = "block";
}
function busy (callback)
{
show ();
setTimeout (callback, 1);
}
function work ()
{
var start = (new Date ()).getTime ();
var wait = 3000;
while ((new Date()).getTime () - start < wait)
{
/* do nothing */
}
hide ();
}
function start ()
{
document.getElementById ("cover").onclick = function (ev) {
document.getElementById ("text").innerHTML = 'Cover clicked';
};
busy (work);
}