【移动端debug-4】iOS下setTimeout无法触发focus事件的解决方案
开篇总结:其实目前无法解决这个bug。
这两天做项目遇到了这个case,项目需求是打开页面的时候,input元素自动弹起键盘。由于各种方面的考虑,我们希望通过setTimeout延时200毫秒让input元素focus,demo代码如下:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>IOS下setTimeout无法触发focus事件的解决方案</title>
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no">
</head>
<body>
<div id="container">
<input id="input-box" type="text" placeholder="click here to focus.">
</div>
<script>
var container = document.getElementById("container");
var input = document.getElementById("input-box");
setTimeout(function () {
input.focus();
},200);
</script>
</body>
</html>
- 问题出在哪?
上面的代码在pc上显示没有问题,但是在安卓上也ok,但是在ios上出了问题,input没有获得焦点,问题出在哪了?
我通过debug发现,代码能执行到setTimeout里面,并且input元素也没有选择失败,那我们判断是input.focus()这句失效了。
- 前人指路
然后我们在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将只允许在其他元素上绑定函数来触发focus事件,如果第一个函数调用栈是由非编程触发的事件(这句不知道怎么翻译)。在你的案例中,调用setTimeout开始一个新的调用堆栈,IOS的安全机制开始阻止你触发input元素的focus事件。
github上也有相关的issue:iOS does not show keyboard on .focus()
里面也有人指出:
iOS won't, as far as I can tell from testing, show the keyboard without some kind of user interaction.Trying a setTimeout to load it doesnt work. But setting the focus on another element's onClick event brings it up.
翻译:据我目前测试所知,如果没有通过某种用户交互,iOS不会(触发focus事件)。用setTimeout试图触发focus不工作(setTimeout是延时触发,非用户交互的当下触发focus),但设置另一个元素的onClick事件,就能把focus事件带起来。
//通过在input以外的其他元素绑定事件可以触发input元素的focus事件
container.addEventListener("click",function(e){
input.focus();
});
- 解决方案?
目前看来没有更好的办法能在iOS下,通过setTimeout调起focus事件,所以只能把setTimeout去掉,从产品设计上避免。
如果你有什么好的解决方案,欢迎留言哦!
【移动端debug-4】iOS下setTimeout无法触发focus事件的解决方案的更多相关文章
- ios下Safari无法触发click事件的处理
ios下的Safari真是傲娇啊,坑好多. 首先上代码 <!DOCTYPE html> <html> <head> <title>122</tit ...
- 【移动端debug-3】部分安卓机型不触发touchend事件的解决方案
最近在项目中遇到一个奇怪的问题,有一个需求是这样:页面上有一个按钮,滚动页面时让它消失,停止滚动时让它显示. 常规思路: step1.监听touchstart事件,记录Touch对象中pageY初始值 ...
- ios下点击穿透focus获取问题
在ios下的浏览器中当点击当前页的一个按钮,用window.location.href进行跳转时,如果下一个页面里这点击按钮的位置是一个textarea或者text等那么他会触发focus事件,会出现 ...
- [转]FireFox与IE 下js兼容触发click事件的代码
本文转自:http://www.jb51.net/article/16549.htm FireFox与IE 下js兼容触发click事件 ,对于需要兼容这两者的朋友,就需要参考下下面的代码了<a ...
- iOS下调用元素的focus方法,input元素不聚焦,键盘不弹起的问题
页面元素 <input type="text" ref="elInput"/> <div style="margin-top:20p ...
- 移动端 常见问题整理 iOS下的 Fixed + Input 调用键盘的时候fixed无效问题解决方案
使用iScroll时,input等不能输入内容的解决方法 <script> function allowFormsInIscroll(){ [].slice.call(document.q ...
- 移动端Web开发,ios下 input为圆角
在处理input的问题时,一般不想要input的原来的样式,一般就直接处理 border: none; outline: none; background: transparent; 这样之后,一般就 ...
- HTML5视频播放在ios下浮动元素无法点击的解决方案
最近许多人在微博上问到在iPad.iPhone.iTouch等设备上使用HTML5播放视频,在视频上方悬浮一个div元素,div内的元素无法点击的情况,而在PC浏览器上没有这个问题. 这个现象是设备特 ...
- 修改ubuntu按下关机键触发的事件
gsettings set org.gnome.settings-daemon.plugins.power button-power shutdown will change your the beh ...
随机推荐
- 安装QConf 报错及解决方案
1:提示找不到gdbm.h头文件 /alidata/QConf/agent/qconf_dump.cc:1:18: fatal error: gdbm.h: No such file or direc ...
- (转载)jQuery判断checkbox是否选中的3种方法(个人用第二种方法)
方法一: if ($("#checkbox-id")get(0).checked) { // do something } 方法二: if($('#checkbox-id' ...
- sqlmap注入分类
注入分法不同,种类不同,来个简单的分类: 1.get型:sqlmap -u “http://xxx.xx.xxx/xx.xxx?xx=xxx” 2.post型: sqlmap -u “http:// ...
- 2018年美国大学生数学建模竞赛(MCM/ICM) B题解题思路
老套路,把我们在解决B题时候采用的思路分享给大家,希望大家能学到点东西~~~ B题思路整理:Part1:先整理出说某种语言多的十个国家给找出来,或者说是把十种语言对应的国家找出来 然后再对各个国家的人 ...
- Android Service(下)
阅读本文需要先阅读Android Service(上) 一 为什么需要bindService() 绑定服务就是为了和服务进行通讯 可以调用服务里面的方法 二 bindService()调用服务里面方法 ...
- python图像处理(2)图像水印和PIL模式转化
模式转化: PIL模式转化:将图片转化成其他模式 # 我们将image图像转化为灰度图像(python) from PIL import Image img = Image.open('c:\\1.J ...
- spring 在ssh三大框架中充当的角色
https://blog.csdn.net/yeah_nn/article/details/79992777
- HTML学习1-Dom之事件绑定
事件: 1.注册事件 a. <div onxxxx=””></div> b. document .onxxxx= function() //找到这个标签 2.this,触发 ...
- fsck命令详解
基础命令学习目录首页 本文出自 “airfish2000” 博客,更多命令查看博客: http://airfish2000.blog.51cto.com/10829608/1880801 fsck ...
- oracle查看用户表
select table_name from user_tables;