search

Home  >  Q&A  >  body text

javascript - Problem with text box onblur event being triggered continuously

text tag onblur="alert()"
Alert() is triggered when the text box loses the cursor;
But when the mouse selects this text, and then the mouse leaves the entire browser window (such as clicking on the desktop), This triggers the alert() event
Now here comes the problem:
After the mouse returns to the browser and clicks the pop-up "OK" button, the pop-up window continues to pop up...
What should I do?

漂亮男人漂亮男人2770 days ago1052

reply all(2)I'll reply

  • 怪我咯

    怪我咯2017-05-19 10:15:27

    This will not happen, please post your code.


    Supplement

    If I can answer it, I must have tried it. There will be no problem with the code below

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>
        <input type="text" onblur="alert();">
    </body>
    </html>

    Has been tested on IE11, Firefox, Chrome


    Supplement

    The situation described does exist. Let’s think about the reasons:

    After switching back, the window loses focus. When it comes back, the text box automatically gains focus, so it continues, thus triggering continuously.

    This kind of problem should actually be relatively rare during use. One solution is to make the text box lose focus when the window loses focus as follows:

    Tested problem solved

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>
        <input type="text" onblur="alert();">
        <script>
            window.onblur=function(){
                document.getElementsByTagName('input')[0].blur();
            }
        </script>
    </body>
    </html>

    reply
    0
  • 黄舟

    黄舟2017-05-19 10:15:27

    I just tried it, and it does exist. However, if you change the alert to something else (such as console.log), blur will not be triggered repeatedly. Under normal circumstances, alert should not be used, so this problem should not be solved. It won't have a big impact. As for why this happens, I don’t know~~

    reply
    0
  • Cancelreply