简体   繁体   English

Chrome发出警报后不会触发模糊事件,但可以在Firefox和IE上触发

[英]Blur event not fired after alert in chrome, but can on Firefox and IE

Here is the code in jsfiddle. 这是jsfiddle中的代码

1.After change text on the textbox, the alert popup appear, click ok, then the blur event is fired and the text change. 1.在文本框中更改文本后,将显示警报弹出窗口,单击“确定”,然后触发模糊事件并更改文本。 That work in Firefox and IE, but in Chrome the blur event is not fired and so, the text not change. 可以在Firefox和IE中使用,但在Chrome中不会触发模糊事件,因此文本不会更改。 Why? 为什么?

2.I observe some curious thing. 2.我观察到一些奇怪的事情。 Change text in textbox, switch to other app (or Ctrl + T new tab), the alert popup appear, click ok, and now the blur event is fired on Chrome. 更改文本框中的文本,切换到其他应用(或Ctrl + T新标签),出现警报弹出窗口,单击“确定”,然后在Chrome上触发模糊事件。 With the isFocus of textbox is false even before the alert, and in blur function. 带有isFocus的文本框甚至在警报之前为false,并且具有模糊功能。 Why is it even fire at all if the textbox has already lost focus? 如果文本框已经失去焦点,为什么甚至会起火呢?

 $(document).ready(function() { $("#id1").val('Original Text'); $("#id1").blur(function() { console.log('When blur fire: Input has focus: ' + $("#id1").is(":focus")); actionBlur(jQuery(this)); }); }); function actionBlur($field) { $field.val("Blur fired"); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <input id="id1" property="prop1" onchange="scriptAlert();" /> <script> var isFocus; function scriptAlert() { isFocus = $("#id1").is(":focus"); console.log('Before alert: Input has focus: ' + $("#id1").is(":focus")); alert('input has Focus: ' + $("#id1").is(":focus")); console.log('After alert: Input has focus: ' + $("#id1").is(":focus")); } </script> 

One small thing - 一件事-

alert('input has Focus: ' + $("#id1").is(":focus");

should be - 应该 -

alert('input has Focus: ' + $("#id1").is(":focus"));

Your Chrome console should show this error. 您的Chrome控制台应显示此错误。 With that fixed, the script worked OK for me in both Firefox and Chrome (Version 51.0.2704.106 m). 修复此问题后,该脚本在Firefox和Chrome(版本51.0.2704.106 m)中都可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM