window.onfocus在IE7中没有触发,在Opera中不一致 [英] window.onfocus not firing in IE7, inconsistent in Opera

查看:90
本文介绍了window.onfocus在IE7中没有触发,在Opera中不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,这与窗口上的焦点和模糊事件有关,而不是与表单字段有关。

Note that this relates to focus and blur events on a window, not on form fields.

我正在弹出窗口中加载文档,并且它包括以下代码:

I'm loading a document in a pop-up window, and it includes the following code:

<script type="text/javascript">
window.onblur = function(){ document.title="BLURRED"; }
window.onfocus= function(){ document.title="FOCUSED"; }
</script>

这些函数是临时的,用于测试。我希望使用这些事件来设置一个指示窗口状态的标志;我正在做一个聊天应用程序,如果消息在最小化时进入,我会做一些注意力的标题更改。如果他们在窗口获得焦点时没有取消,那么他们就会很烦人。

These functions are temporary, for testing. I'd hoped to use these events to set a flag indicating the window state; I'm doing a chat app, and if messages come in when it's minimized I'll do some attention-getting title changes. If they don't cancel when the window gets focus, though, they'll just be annoying.

Onload,我也把焦点放在textarea上。 (不确定这是否有所不同。)

Onload, I also put focus into a textarea. (Not sure if that makes any difference.)

IE7(我没有其他版本的方便)似乎认识到 window.onblur 但不是 window.onfocus 。 Opera 10简直太奇怪了。

IE7 (I don't have another version handy) seems to recognise the window.onblur but not the window.onfocus. Opera 10 is just downright strange.

这就是我所拥有的浏览器中发生的事情。我通过单击父级中的链接启动弹出窗口,然后通过单击(Windows XP)任务栏上的弹出按钮来执行几个最小化还原周期:

Here's what happens in the browsers I have. I launch the pop-up window by clicking on the link in the parent, then go through several minimize-restore cycles by clicking the popup's button on the (Windows XP) taskbar:

Safari 4:


  • 打开窗口:文档以原始标题打开

  • 最小化:标题更改为BLURRED

  • 恢复:标题更改为FOCUSED

这就是我预期会发生的事情。

This is what I expected to happen.

Firefox 3.5:


  • 打开窗口:文档以原始标题打开,然后更改为FOCUSED

  • 最小化:标题更改为BLURRED

  • 恢复:标题更改为FOCUSED

onfocus onload有点令人惊讶,但不是问题。

The onfocus onload is a bit of a surprise, but not a problem.

IE7:


  • 打开窗口:文档打开原始标题

  • 最小化:标题更改为BLURRED

  • 恢复:标题不会改变,无论我切换多少次

onfocus发生了什么?

What happened to onfocus?

Opera 10.5


  • 打开窗口:文档以原始标题打开,但在另一个标签中,不弹出

有时......


  • 最小化:标题更改为BLURRED

  • 恢复:标题更改为FOCUSED

有时...


  • 最小化:标题更改为BLURRED,然后FOCUSED

  • 恢复:标题不会改变

好的,这很奇怪......

Okay, this is just plain weird...

我对其他方法持开放态度,但我真的很想知道这里发生了什么,用普通的旧Javascript - 所以请不要给jQuery回答除非那里有真的没有办法解决这个问题。

I'm open to other approaches, but I'd really like to understand what's going on here, in plain old Javascript - so please don't give a jQuery answer unless there really is no other way around this.

谢谢!

推荐答案

onfocus onblur 在IE中的窗口对象上有错误。另一种方法是使用传播 onfocusin onfocusout 事件:

onfocus and onblur are buggy on the window object in IE. The alternative is to use the propagating onfocusin and onfocusout events:

function focusin() { document.title="BLURRED"; }
function focusout() { document.title="FOCUSED"; }

if ("onfocusin" in document)
    document.onfocusin = focusin,
    document.onfocusout = focusout;
else
    window.onblur = focousout,
    window.onfocus= focusin;

我为你设置了一个例子这里

I've set up an example for you here.

focusin 焦点,不像焦点模糊,正在传播事件;他们将触发页面中的元素并向上冒泡。如果您不想对页面上的所有元素执行此事件,则需要检查 event.srcElement event.target

focusin and focusout, unlike focus and blur, are propagating events; they will fire for elements in the page and bubble upwards. You will need to check the event.srcElement or event.target if you don't want to act on this event for all elements on the page.

至于Opera,奇怪是你可以使用的一个词。我的机器上的版本不会为我启动窗口上的模糊或焦点事件。希望其他人能为您提供解决方案。

As for Opera, "strange" is one word you could use. The version on my machine will not fire the blur or focus events on the window for me. Hopefully someone else can offer you a solution for that.

这篇关于window.onfocus在IE7中没有触发,在Opera中不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆