问题
jQuery's focus() method is does not appear to work when used from within a setTimeout in iOS.
So,
setTimeout( function () {
// Appears to have no effect in iOS, fine in Chrome/Safari/Firefox/IE
$('.search').focus();
}, 500);
But on it's own,
// works fine.
$('.search').focus();
See the following example:
http://jsfiddle.net/nwe44/ypjkH/1/
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?
回答1:
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');
}
回答2:
For my app it works, if you just put the x.focus() into a(nother) separate function and call it from within the setTimeout
来源:https://stackoverflow.com/questions/6866433/focus-not-working-within-settimeout-on-ios