$.focus() not working

后端 未结 14 1186
走了就别回头了
走了就别回头了 2020-11-27 02:48

The last example of jQuery\'s focus() documentation states

$(\'#id\').focus()

should make the input focused (active). I can\'t seem to get

14条回答
  •  佛祖请我去吃肉
    2020-11-27 03:04

    Actually the example you gave for focusing on this site works just fine, as long as you're not focused in the console. The reason that's not working is simply because it's not stealing focus from the dev console. If you run the following code in your console and then quickly click in your browser window after, you will see it focus the search box:

    setTimeout(function() { $('input[name="q"]').focus() }, 3000);
    

    As for your other one, the one thing that has given me trouble in the past is order of events. You cannot call focus() on an element that hasn't been attached to the DOM. Has the element you are trying to focus already been attached to the DOM?

提交回复
热议问题