如果将光标放在第一个输入中,然后单击按钮,您会收到一条消息。
如果将光标放在第二条消息上(使用 setTimeout
),您会收到两条消息。
为什么在第一个场景中不显示“单击按钮”消息?
相反,setTimeout
是什么让第二个同时显示这两个警报?
<html>
<head>
<script type='text/javascript'>
function testOne() {
alert('button was clicked');
}
function testTwo() {
alert ('focus left the input');
}
</script>
</head>
<body>
<input name="input1" onblur="testTwo()" />
<input name="input2" onblur="setTimeout(function(){testTwo();}, 100)" />
<button name="button1" onclick="testOne()">Button</button>
</body>
</html>
最佳答案
你认为正在发生的事情,实际上并没有发生。
尝试使用 console.log
(不会阻塞)而不是 alert
(会阻塞),您将看到两个事件处理程序都在运行。万岁,又一个鄙视 alert
调试的理由。
关于javascript - onBlur 杀死 onClick 但如果使用 setTimeout 则不会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6391997/