简体   繁体   English

当输入类型为数字时,Firefox 会自动触发 focusout/blur 事件

[英]Firefox fires focusout / blur event automatically when input type is number

I have a JavaScript code running perfectly in all browsers except Firefox.我有一个 JavaScript 代码,可以在除 Firefox 之外的所有浏览器中完美运行。 I am running it in Firefox Quantum Version 66.0.2 (64-bit).我在 Firefox Quantum 版本 66.0.2(64 位)中运行它。 I have three HTML input controls, all have their display style set to none.我有三个 HTML 输入控件,它们的显示样式都设置为无。 Clicking on a link shows first input.单击链接显示第一个输入。 On focusout event of first input, first input hides and second input becomes visible.在第一个输入的焦点事件时,第一个输入隐藏,第二个输入变为可见。 Similarly on focusout of second input, second input hides and third becomes visible.同样,在第二个输入的焦点上,第二个输入隐藏,第三个输入可见。

 function showFirst() { //Show first input $('#first').css('display', 'inline'); $('#first').focus(); } $(document).ready(function () { $('#first').focusout(function (event) { //Write log $('body').append('first focusout <br/>'); //hide itself event.currentTarget.style.display = 'none'; //show next input ie second $('#second').css('display', 'inline'); $('#second').focus(); }); $('#second').focusout(function (event) { //Write log $('body').append('second focusout <br/>'); //hide itself event.currentTarget.style.display = 'none'; //show next input ie third $('#third').css('display', 'inline'); $('#third').focus(); }); $('#third').focusout(function (event) { //Write log $('body').append('third focusout <br/>'); //hide itself event.currentTarget.style.display = 'none'; //show next input ie first $('#first').css('display', 'inline'); $('#first').focus(); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!--It will show first input--> <a href="#" onclick="showFirst();">Click to show first</a> <!--Its focusout event will hide it and will show second input--> <input type="text" id="first" value="" style="display:none;background-color:yellow; color:black;" placeholder="First" /> <!--Its focusout event will hide it and will show third input--> <input type="number" id="second" value="" style="display:none;background-color:red; color:white;" placeholder="Second" /> <!--Its focusout event will hide it and will show first input--> <input type="number" id="third" value="" style="display:none;background-color:green; color:white;" placeholder="Third" /> <br /> <br /> <a href="#">&nbsp;</a> <!--Just to receive focus-->

The Problem:问题:

When 'Click to show first' is clicked, it shows 'first' input.单击“单击以首先显示”时,将显示“第一个”输入。 When tab is pressed then focusout events of 'second' and 'third' are fired by itself and 'second' and 'third' inputs are never shown.当按下 Tab 键时,'second' 和 'third' 的焦点事件会自行触发,并且永远不会显示 'second' 和 'third' 输入。 Interestingly if input type of all is changed from number to text then same code works fine.有趣的是,如果 all 的输入类型从数字更改为文本,则相同的代码可以正常工作。

Open this JSFiddle in Firefox to see it in action.在 Firefox 中打开这个 JSFiddle以查看它的运行情况。

You need to set the listener for the next input after trigger the previous input focusout event.触发上一个输入焦点事件后,您需要为下一个输入设置侦听器。

Try the below code.试试下面的代码。

 function showFirst() { //Show first input $('#first').css('display', 'inline'); $('#first').focus(); } $(document).ready(function () { $('#first').focusout(function (event) { //Write log $('body').append('first focusout <br/>'); //hide itself event.currentTarget.style.display = 'none'; //show next input ie second $('#second').css('display', 'inline'); $('#second').focus(); $('#second').focusout(function (event) { //Write log $('body').append('second focusout <br/>'); //hide itself event.currentTarget.style.display = 'none'; //show next input ie third $('#third').css('display', 'inline'); $('#third').focus(); $('#third').focusout(function (event) { //Write log $('body').append('third focusout <br/>'); //hide itself event.currentTarget.style.display = 'none'; //show next input ie first $('#first').css('display', 'inline'); $('#first').focus(); }); }); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!--It will show first input--> <a href="#" onclick="showFirst();">Click to show first</a> <!--Its focusout event will hide it and will show second input--> <input type="text" id="first" value="" style="display:none;background-color:yellow; color:black;" placeholder="First" /> <!--Its focusout event will hide it and will show third input--> <input type="number" id="second" value="" style="display:none;background-color:red; color:white;" placeholder="Second" /> <!--Its focusout event will hide it and will show first input--> <input type="number" id="third" value="" style="display:none;background-color:green; color:white;" placeholder="Third" /> <br /> <br /> <a href="#">&nbsp;</a> <!--Just to receive focus-->

Finally, I am able to solve it.最后,我能够解决它。 It is a bug in Firefox version 60, 66, 67 and 68. See bug reports in bugzilla here and here .这是 Firefox 版本 60、66、67 和 68 中的一个错误。请在此处此处查看 bugzilla 中的错误报告。 "Alice0775 White" suggested here to add timeout to focus. “Alice0775 White”建议在这里添加超时以聚焦。 I just added timeout and it started working.我刚刚添加了超时并开始工作。 Following is the working code:以下是工作代码:

    $(document).ready(function () {
        $('#first').focusout(function (event) {
            $('body').append('first focusout <br/>');
            event.currentTarget.style.display = 'none';
            $('#second').css('display', 'inline');

            //add timeout to focus
            setTimeout(function () {
                $('#second').focus();
            }, 0)
        });
        $('#second').focusout(function (event) {
            $('body').append('second focusout <br/>');
            event.currentTarget.style.display = 'none';
            $('#third').css('display', 'inline');

            //add timeout to focus
            setTimeout(function () {
                $('#third').focus();
            }, 0)
        });
        $('#third').focusout(function (event) {
            $('body').append('third focusout <br/>');
            event.currentTarget.style.display = 'none';
            $('#first').css('display', 'inline');

            //add timeout to focus
            setTimeout(function () {
                $('#first').focus();
            }, 0)
        });
    });

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

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