1 回答

TA贡献1878条经验 获得超4个赞
安卓我目前没有遇到问题。
然后直接使用input.focus()是无法在ios中调起键盘的,因为ios中input元素的focus必须由事件触发。
此外,如果模拟了触摸事件,但是在setTimeout中延迟执行,也是不行的。这部分可以参考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并调起键盘,可以将focus调用包装在一个用户行为触发的事件中,如点击事件、表单onChange事件等。
解决方案
如果需要模拟触发事件,可以参考manually trigger touch event
添加回答
举报