在iOS的setTimeout中,focus()无法正常工作
问题描述:
在iOS的setTimeout中使用jQuery的focus()方法似乎无法正常工作.
jQuery's focus() method is does not appear to work when used from within a setTimeout in iOS.
所以
setTimeout( function () {
// Appears to have no effect in iOS, fine in Chrome/Safari/Firefox/IE
$('.search').focus();
}, 500);
但就其本身而言,
// works fine.
$('.search').focus();
请参见以下示例:
http://jsfiddle.net/nwe44/ypjkH/1/
如果在setTimeout之外进行focus()调用,则它起作用,而在setTimeout内,则不起作用.就像其他方法一样起作用,这让人感到双重好奇.例如,在我的jsFiddle中,我可以更改边框颜色,而不必关注它.有任何想法吗?
If the focus() call is made outside the setTimeout it works, inside it doesn't. This is doubly curious as other methods do work. For example, in my jsFiddle I'm able to change the border color, just not focus it. Any ideas?
答
检查小提琴我已经更新了 http://jsfiddle.net/ypjkH/7/
Check fiddle i have updated at http://jsfiddle.net/ypjkH/7/
$('#selector').click( function (e) {
e.preventDefault();
setTimeout( doFocus
, 3000);
});
function doFocus() {
$('.search').focus().css('border', '1px solid red');
}