很不幸的,不行,鍵盤只能靠使用者行為觸發,不能通過腳本觸發,測試過 ios 和 android 確實是如此。 而所謂的使用者行為只可以通過綁定事件觸發,而且必須在觸發當前的 context: 例如 <input type="text" id="input"> <button id="click">Click</button> // 只能通過使用者行為觸發 document.getElementById('click').addEventListener('click', function() { document.getElementById('input').focus() // 這樣就會成功開啟鍵盤並焦點在 input setTimeout(function() { document.getElementById('input').focus() // 這樣就會失敗 }, 2000) }) 測試了幾次發現,只要 document.getElementById('input').focus() 在 callback 裡面都會失敗
很不幸的,不行,鍵盤只能靠使用者行為觸發,不能通過腳本觸發,測試過
ios
和android
確實是如此。而所謂的使用者行為只可以通過綁定事件觸發,而且必須在觸發當前的
context
:例如
測試了幾次發現,只要
document.getElementById('input').focus()
在callback
裡面都會失敗