我有一个 onblur 事件,如果输入不正确的数据,该事件将导致弹出错误窗口。问题是,当我尝试关闭网页或单击任何其他窗口时,onblur 事件将激活并且弹出窗口将再次出现...有人知道如何解决此问题吗?
这是代码的简化版本
<input id="M3_pos_tab" type="text" maxlength="5" name="Blank" size="5" onblur="CheckFS();"/>
<script type="text/javascript">
function CheckFS()
{
var FS_pos = parseFloat(FS_tab.value);
if ((FS_pos == 0) || (FS_pos == parseFloat(L_tab.value)) || (isNaN(FS_pos))) {
} else {
window.alert("Error");
FS_tab.select();
FS_tab.focus();
return;
}
}
最佳答案
我认为你的问题是,当错误框出现时,它会获得焦点,因此会在元素上触发 onblur 。我在 https://javascript.info/focus-blur 找到了这个,其中表示
A focus loss can occur for many reasons. One of them is when the visitor clicks somewhere else. But also JavaScript itself may cause it, for instance:
- An alert moves focus to itself, so it causes the focus loss at the element (blur event), and when the alert is dismissed, the focus comes back (focus event).
因此我建议删除该行
window.alert("Error");
并查看问题是否仍然存在。
关于javascript - Onblur功能持续运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17528932/