Html Webkit:在iOS WKWebView中呈现的文件输入选择器不能在一次点击下工作
我正在使用一个名为的前端javascript库来构建一个web应用程序,它看起来和感觉上都像一个原生的iOS/Android应用程序 我遇到了这种奇怪的行为,我必须双击某些按钮才能进行单击操作。从我的发现来看,这种奇怪的行为只发生在我的自定义输入按钮(即自定义文件上载按钮、自定义复选框按钮等)上 为了提供更多的上下文,我隐藏了浏览器呈现的不太令人愉快的默认文件Html Webkit:在iOS WKWebView中呈现的文件输入选择器不能在一次点击下工作,html,ios,onclick,webkit,wkwebview,Html,Ios,Onclick,Webkit,Wkwebview,我正在使用一个名为的前端javascript库来构建一个web应用程序,它看起来和感觉上都像一个原生的iOS/Android应用程序 我遇到了这种奇怪的行为,我必须双击某些按钮才能进行单击操作。从我的发现来看,这种奇怪的行为只发生在我的自定义输入按钮(即自定义文件上载按钮、自定义复选框按钮等)上 为了提供更多的上下文,我隐藏了浏览器呈现的不太令人愉快的默认文件元素,而是显示自己的界面,用于打开文件选择器并显示用户想要上传的文件 您可以通过使用display:none设置输入元素的样式,并调用元素
元素,而是显示自己的界面,用于打开文件选择器并显示用户想要上传的文件
您可以通过使用display:none
设置输入元素的样式,并调用元素上的click()
方法来实现这一点,如下所述:
当我从mobile Safari应用程序访问我的web应用程序时,只需轻触一下即可工作。但是在内部,我必须快速点击两次才能调用iOS摄像头/文件选择器
下面是Onsen UI web应用程序中的HTML、CSS和JS片段
HTML:
<form>
<label>
<ons-button id="upload-button" type="button">Upload Photo
<input id="image-selector" name="document_front" type="file" style="display: none" accept="image/*" capture="camera" />
</ons-button>
</label>
</form>
Javascript:
document.querySelector('#upload-button').onclick = function () {
document.querySelector('#image-selector').click();
作为测试,我将控制台上的点击记录下来。我发现,Safari和Chrome(这些浏览器的移动版本也是如此)中启动了“点击图像选择器”,但WKWebView中没有启动
在花了几个小时浏览互联网之后,这似乎是由于webkit中的一个bug造成的。我能做些什么来解决这个问题
谢谢你在我们的客户PWA的早期,我们只有在iOS设备上的Safari中才会遇到一个非常恼人的问题。有些按钮没有响应,有时需要多次点击才能最终响应。最后,我们进行了3项更改以成功解决此问题
document.querySelector('#upload-button').onclick = function () {
document.querySelector('#image-selector').click();
- The Annoying Mobile Double-Tap Link Issue | CSS-Tricks
https://css-tricks.com/annoying-mobile-double-tap-link-issue/
- Ensuring that your UI element is on top (z-index values) and using the
“cursor” property of the CSS class will get you “on hover” cursor effects
without using the pseudo element.
- This one is especially annoying as it often means you need to create or
get image assets that have the desired background color built in.
- I would say it is OK to only worry about this one if you have reports of
your button not being responsive.
This is mostly a testing item. Test that when tapping at the edge of your
button, not directly on the image or text, the button still activates.
If it sometimes does not, then consider implementing the fixes above.