移动Safari自动对焦文本字段
在Mobile Safari中,在设置延迟时间后,我无法将注意力集中在文本字段上。我附上一些展示问题的示例代码。如果单击该按钮,则触发.focus(),一切都按预期工作。如果你把焦点放在回调上,比如setTimeout函数,那么它只能在移动safari中失败。在所有其他浏览器中,有一个延迟,然后焦点发生。
令人困惑的是,即使在移动游猎中,也会触发“focusin”事件。这个(和SO中的〜类似〜评论)让我觉得它是一个移动的safari bug。任何指导都将被接受。
我已经在模拟器和iPhone 3GS / 4 iOS4上进行了测试。
示例HTML:
<!DOCTYPE html> <html lang='en'> <head> <title>Autofocus tests</title> <meta content='width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0' name='viewport'> <meta content='yes' name='apple-mobile-web-app-capable'> </head> <body> <h1> Show keyboard without user focus and select text: </h1> <p> <button id='focus-test-button'> Should focus on input when you click me after .5 second </button> <input id='focus-test-input' type='number' value='20'> </p> <script type="text/javascript"> //<![CDATA[ var button = document.getElementById('focus-test-button'); var input = document.getElementById('focus-test-input'); input.addEventListener('focusin', function(event) { console.log('focus'); console.log(event); }); button.addEventListener('click', function() { // *** If triggered immediately - functionality occurs as expected // input.focus(); // *** If called by callback - triggers the focusin event, but does not bring up keyboard or cursor var t = setTimeout("input.focus();",500); }); //]]> </script> </body> </html>
跃然一笑
慕哥6287543
SMILET
相关分类