Can't Set Focus in Safari

前端 未结 1 865
渐次进展
渐次进展 2020-12-20 08:46

I\'m trying to set focus to a particular edit field when a page opens or button is pressed. The following simple HTML/Javascript (Test.html) works with Firefox and Chrome b

相关标签:
1条回答
  • 2020-12-20 09:25

    EDITED TO INCLUDE .focus()

    Try

    javascript:document.getElementById('identNum').focus().setSelectionRange(0, 999);
    

    Mobile devices, in particular iOS can be quite funny about select();

    https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setSelectionRange

    EDIT: A note on input focus without user interaction

    As found by @zappullae

    It appears that in recent versions of iOS, Apple require user interaction in order to activate the keyboard, so this will not be possible on page load.

    https://medium.com/@brunn/autofocus-in-ios-safari-458215514a5f

    0 讨论(0)
提交回复
热议问题