问题描述:最近开发 H5 公众号 页面的时候有一个需求,新建一个页面,顶部是搜索框,下方是搜索的内容,本来写的好好的,在测试的时候 安卓 系统的手机点击搜索框会立即弹出系统键盘,但是 IOS 系统的手机点击输入框时,不能立即弹出系统键盘、重复点击时会弹出键盘
首先需要判断输入框不能点击的原因:是否层级过低导致、是否尺寸过低导致
解决办法:
1、首先下载依赖包 FastClick
npm install fastClick
2、找到 fastClick 依赖包
// 文件路径
node_modules/fastclick/lib/fastclick.js
3、修改 fastclick.js 文件中的内容
// 搜索 FastClick.prototype.focus
// 在 if 中 添加以下代码
targetElement.focus()
// 全部代码:
FastClick.prototype.focus = function(targetElement) {
var length;
if ( deviceIsIOS && targetElement.setSelectionRange &&
targetElement.type.indexOf('data') !== 0 &&
targetElement.type !== 'time' &&
targetElement.type !== 'mouth') {
length = targetElement.value.length;
// 需要添加的代码 开始
targetElement.focus()
// 需要添加的代码 结束
targetElement.setSelectionRange(length, length);
} else {
targetElement.focus()
}
}
4、打包测试、这样 IOS 系统的用户点击输入框就不会有延迟了
当然:原因可能有很多,这只是对应我项目上的问题,这个解决办法是ok的,也有其他的办法,比如:在 input 的标签下添加 这个样式属性:-webkit-user-select:none; 但是在我项目上不管用 修改fastClick.js 文件可以解决。