gpt4 book ai didi

由于页面外点击,Jquery 事件触发了两次?

转载 作者:行者123 更新时间:2023-12-01 04:37:48 25 4
gpt4 key购买 nike

我知道对此有很多疑问,但我的问题略有不同。

我有几个必须使用的输入字段

$('body').on('blur', '#fielda, #fieldb, #fieldc, #fieldd', function(){
alert ('hello')
});

如果我单击fielda,输入一个值并单击,我会收到一个警报,

但是,如果我单击 fielda,然后单击浏览器中未触发第一个警报的另一个应用程序,我会清除该应用程序,然后收到新警报。

我该如何阻止这种情况发生。我设置了一个简单的 fiddle 来展示这一点。

谢谢

最佳答案

尝试 document.hasFocus()

The Document.hasFocus() method returns a Boolean value indicating whether the document or any element inside the document has focus. This method can be used to determine whether the active element in a document has focus.

$('body').on('blur', '#fielda', function() {
if (document.hasFocus()) {
alert('hello')
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="fielda" />

关于由于页面外点击,Jquery 事件触发了两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46442647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com