focus() not working within setTimeout on iOS

点点圈 提交于 2020-12-02 06:12:00

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!