最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google chrome causing infinite loop on textbox focus - Stack Overflow

programmeradmin3浏览0评论

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

<input type="text" />

$('input[type="text"]').live('focus', function(event) {
    alert('in');
});

/

Firefox and IE8 are both fine.

Why is this happening in chrome?

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

<input type="text" />

$('input[type="text"]').live('focus', function(event) {
    alert('in');
});

http://jsfiddle.net/XppG9/

Firefox and IE8 are both fine.

Why is this happening in chrome?

Share Improve this question edited Jul 29, 2011 at 6:27 Shamim Hafiz - MSFT 22.1k44 gold badges120 silver badges181 bronze badges asked Jul 29, 2011 at 6:23 ajbeavenajbeaven 9,56213 gold badges83 silver badges127 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 9

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

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.

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/

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();
});

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)

  1. 暂无评论