在移动版Safari中,我无法在设置延迟时间后将焦点放在文本字段上。以下是一些示例代码展示了这个问题。
如果你在按钮的onclick事件中触发.focus(),那么一切都按预期工作。但是,如果你将焦点挂在一个回调函数上,比如setTimeout函数,那么它只会在移动版Safari中失败。在所有其他浏览器中,会有一个延迟,然后焦点就会出现。
令人困惑的是,即使在移动版Safari中,“focusin”事件也会触发。这(以及Stack Overflow中类似的评论)让我认为这是一个移动版Safari的bug。欢迎任何指导。
我已经在模拟器和iPhone 3GS/4 iOS4上进行了测试。
示例HTML:
如果你在按钮的onclick事件中触发.focus(),那么一切都按预期工作。但是,如果你将焦点挂在一个回调函数上,比如setTimeout函数,那么它只会在移动版Safari中失败。在所有其他浏览器中,会有一个延迟,然后焦点就会出现。
令人困惑的是,即使在移动版Safari中,“focusin”事件也会触发。这(以及Stack Overflow中类似的评论)让我认为这是一个移动版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>
类似的SO问题: