本文介绍了Firefox奇怪的onblur行为(3.6.12)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此处查看测试页 ,当任何一个事件处理程序被调用时增加一个计数器,并且在其他两个之后增加第二个提醒框。尽管浏览器的概念上是单线程模型,但Firefox一旦从焦点处理程序中的警告框打开,Firefox实际上就开始执行模糊处理程序。但是,当第一个(焦点)警报框关闭时,第二个(模糊)警报框也必须在第三个(焦点之后)警报框打开之前关闭。



我不知道Firefox为什么这样做。 Chrome会一次又一次地触发焦点处理程序,Opera,Safari和IE会按照您所期望的方式执行操作。没有拿走文本框的焦点。



编辑:我找到,并将在此发布此测试用例。显然它曾经更糟。

Check the test page here http://rickchristie.com/testfocus.html

Correct me if I'm wrong:

  1. onfocus happens when the element gains focus.
  2. onblur happens when the element loses the focus.

In Firefox 3.6.12 (tested on mac and windows), using onblur without onfocus works fine. However when an input element has both onfocus and onblur element, it gets weird:

  1. When you click at the input, onblur is fired instead of onfocus.
  2. Right after onblur, onfocus fires immediately.
  3. You can't type anything in the textbox.

Other browsers (Safari, Opera) works as expected. Is this a bug? Is it just me or is everybody using Firefox experiencing this?

Update

This behavior seems to happen only when onblur and onfocus contains alert - not when I use something like window.log.

解决方案

When the alert box opens, focus is removed from the text box. Notice that the "BLUR" alert box opens right on top of the "FOCUS" alert box (the latter opened first), so that's why it looks like onblur fired before onfocus.

Look at my test page, which increments a counter when either event handler is called and also adds a second alert box after both of the others. It seems that Firefox actually starts executing the blur handler as soon as the alert box from the focus handler opens, despite the conceptually single-threaded model of the browser. However, when the first (focus) alert box is closed, the second (blur) alert box has to also be closed before the third (after focus) alert box opens.

I don't know Firefox why does it the way it does. Chrome keeps firing the focus handler again and again, and Opera, Safari, and IE do it the way you would expect — not taking away the text box's focus.

EDIT: I've found https://bugzilla.mozilla.org/show_bug.cgi?id=31889 and will be posting this test case there – apparently it used to be even worse.

这篇关于Firefox奇怪的onblur行为(3.6.12)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 23:29