窗口焦点和模糊事件不会对Android浏览器工作正常 [英] window focus and blur events not working correctly on Android browser

查看:196
本文介绍了窗口焦点和模糊事件不会对Android浏览器工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现JavaScript的重点和模糊事件不会对Android浏览器,当连接到窗口,文件或身体正常闪光。

I found out that javascript focus and blur events does not fire correctly on the Android browser, when attached to window, document or body.

我又写道一个简单的测试脚本,运行正常的桌面浏览器,但无法在Android股票浏览器,海豚,和Opera移动:

I worte a simple test script which is working correctly on desktop browsers, but fails on Android stock browser, Dolphin, and Opera mobile:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0" />
    <title>Focus test</title>

    <script type="text/javascript">

        window.onfocus = function() {
            document.getElementById('console').innerHTML += '<div>Focus event handler fired.</div>';
        };
        window.onblur = function() {
            document.getElementById('console').innerHTML += '<div>Blur event handler fired.</div>';
        };

    </script>
</head>

<body>
    <input id="test" name="test" />
    <div id="console"></div>
</body>

</html>

Intresting是,如果表单输入越来越焦点,事件处理程序便会启动,并在模糊,模糊事件处理程序闪光两次。

Intresting is, that if the form input is getting focus, the event handler fires, and on blur, the blur event handler fires twice.

有没有人有一个很好的解决方案或解决方法吗?

Does anyone have a good solution or a workaround for this?

编辑:预期的结果将是,如果我换一个浏览器标签页,或者切换到另一个应用程序,模糊事件应该火了,如果我回到浏览器选项卡,将重点事件应该火(这就是它的工作原理在桌面上)

The expected result would be, that if I change a browser tab, or change to another app, the blur event should fire, and if I go back to the browser tab, the focus event should fire (that's how it works on desktop)

推荐答案

我刚刚测试在Galaxy Nexus的运行软糖你的code(我的全部),这code可以完美运行,而且两个事件只火每一次

I just tested your code on a Galaxy Nexus running jellybean (all I have) and this code works perfect, and both events only fire once each.

document.getElementById('test').onfocus = function() {
    document.getElementById('console').innerHTML += '<div>Focus event handler fired.</div>';
};
document.getElementById('test').onblur = function() {
    document.getElementById('console').innerHTML += '<div>Blur event handler fired.</div>';
};​

可能是因为你的目标窗口,这对大多数移动设备,通常不能模糊每本身。确保你直接问元素来报告自己的模糊和焦点事件。

Could be that you were targeting the window, which on most mobile devices, cannot normally be "blurred" per-se. Make sure you directly ask the element to report its own blur and focus events.

编辑: 此外,请确保你等待DOM来准备申请事件侦听器了。

Also, make sure you're waiting for the DOM to be ready before applying event listeners.

这篇关于窗口焦点和模糊事件不会对Android浏览器工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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