1 回答

TA貢獻1878條經(jīng)驗 獲得超4個贊
安卓我目前沒有遇到問題。
然后直接使用input.focus()是無法在ios中調(diào)起鍵盤的,因為ios中input元素的focus必須由事件觸發(fā)。
此外,如果模擬了觸摸事件,但是在setTimeout中延遲執(zhí)行,也是不行的。這部分可以參考stackoverflow上的case:Mobile Safari Autofocus text field.
來自FastClick團隊的大牛指出了IOS下input的獲取焦點存在這樣的問題:
my colleagues and I found that iOS will only allow focus to be triggered on other elements, from within a function, if the first function in the call stack was triggered by a non-programmatic event. In your case, the call to setTimeout starts a new call stack, and the security mechanism kicks in to prevent you from setting focus on the input.
綜上,要在ios中選中input并調(diào)起鍵盤,可以將focus調(diào)用包裝在一個用戶行為觸發(fā)的事件中,如點擊事件、表單onChange事件等。
解決方案
如果需要模擬觸發(fā)事件,可以參考manually trigger touch event
添加回答
舉報