移动端input 无法获取焦点的问题

时间:2024-01-03 08:52:50

下午遇到一个问题,移动端的input都不能输入了,后来发现是

-webkit-user-select :none ;

在移动端开发中,我们有时有针对性的写一些特殊的重置,比如:

* {
-webkit - touch - callout: none;
//-webkit-touch-callout:none; 阻止长按图片之后呼出菜单提示复制的行为

//禁用Webkit内核浏览器的文字大小调整功能。
-webkit-text-size-adjust: none;

//避免点击a标签或者注册了click事件的元素时产生高亮
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
//
//禁止用户进行复制.选择.
-webkit-user-select: none;
}

其中,-webkit-user-select :none ;会产生一些问题。
这是webkit内核浏览器下的一个bug,具体可以参考这篇文章:https://bugs.webkit.org/show_bug.cgi?id=82692

移动端input 无法获取焦点的问题

阻止了用户的选择内容行为,会导致一些“内容可编辑”标签无法正常使用,比如input、testarea。

如果网站不需要阻止用户的选择内容的行为就可以使用如下样式:

  * {
-webkit-user-select: text;
-user-select: text;
}

另一种方式:

*: not(input, textarea) {
-webkit - touch - callout: none;
-webkit - user - select: none;
}

user-select , can cause issues in elements with contenteditable="true" ,so better to add that too .

所以,最好把它也加上。

最终的代码:

[contenteditable = "true"], input, textarea {
-webkit-user- select: auto!important;
-khtml-user-select: auto!important;
-moz-user-select: auto!important;
-ms-user-select: auto!important;
-o-user-select: auto!important;
user-select: auto!important;
}

本文内容大概就这么多,欢迎交流,欢迎反馈,如有错误,还请纠正,谢谢阅读。

附参考链接:
http://*.com/questions/12812587/phonegap-styles-webkit-user-select-none-disabling-text-field