Google chrome causing infinite loop on textbox focus

前端 未结 5 1144
孤街浪徒
孤街浪徒 2021-01-18 05:39

I\'m getting some strange happenings on google chrome. With the following code I\'m getting an infinite number of alerts.

<

相关标签:
5条回答
  • 2021-01-18 06:18

    I think, it's because after you close the dialog (alert box), the focus returns on the textbox, therefore, the function will fire again.

    0 讨论(0)
  • 2021-01-18 06:18

    This is happening because it is setting focus back to the text box. Try this it should work fine in Chrome

    $('input[type="text"]').live('focus', function(event) {
        alert('in');
        $(this).blur();
    });
    
    0 讨论(0)
  • 2021-01-18 06:25

    The problem is that the alert() is stealing focus from the input box, and then restoring it when the dialog is closed. You can fix this by clearing focus from the input box before you show the alert.

    Example: http://jsfiddle.net/XppG9/6/

    0 讨论(0)
  • 2021-01-18 06:26

    Because alert is getting the focus from your text box, and on closing the alert dialog,focus gets back. If you do any non focusing mechanism inside your function , it will trigger only once : http://jsfiddle.net/G8CmV/

    <input type="text" />
    <div id='tester'>Test:</div>
    
    
    
     $('input[type="text"]').live('focus', function(event) {
        $('#tester').html( $('#tester').html() + "_*" );
      });
    
    0 讨论(0)
  • 2021-01-18 06:28

    I think it's because the the browser is sending focus from the alert to your text field every time you click the alert's "OK" button. You're probably not going to be popping up an alert (methinks) in the final version of your code, so this might not be an issue in the long run.

    0 讨论(0)
提交回复
热议问题