iOS 9上的Safari不会触发隐藏输入文件上的单击事件 [英] Safari on iOS 9 does not trigger click event on hidden input file

查看:137
本文介绍了iOS 9上的Safari不会触发隐藏输入文件上的单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个上传字段的网站,但输入隐藏了 display:none; 和I有一个 div 用于调用此操作。

I have a website with an upload field, but the input is hidden with display: none; and I have a div for calling this action.

它适用于iOS 8,但现在在iOS 9更新后,当我触摸div时没有任何反应。

It worked on iOS 8, but now after the iOS 9 update, nothing happens when I touch in the div.

我尝试过使用 [0] .click()或纯粹的VanillaJS,如 document.getElementById('file_input')。click(),一切正常。

I've tried using the [0].click() or pure VanillaJS like document.getElementById('file_input').click() and nothing works.

所有这些方法在iOS 5和iOS 8之间工作。

All these methods work between iOS 5 and iOS 8.

如果需要,可以链接到JSFiddle上的这个示例: https://jsfiddle.net/o1r265tz/6/embedded/result/

$(document).ready(function(){

  $(document).on('click touchstart', '.upload-box', function(e){
    e.stopPropagation();
    $('.form-upload input.file-input').trigger('click');

    $('.debug').append('<p>Clicked!</p>');
    console.log('Clicked!');
    return false;
  });

});

.upload-box {
  border: 3px solid #999;
  padding: 10px;
  text-align: center;
  border-radius: 5px;
  font-size: 35pt;
  font-family: Arial;
  color: #555;
}
.form-upload {
  display: none;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="upload-box">Click here to upload =D</div>

<form class="form-upload" enctype="multipart/form-data">
  <input class="file-input" id="file_input" type="file">
</form>

<div class="debug"></div>

推荐答案

有三件事导致了这个问题:

Three things are causing this problem:


  1. 在javascript中,删除返回false; 事件监听器。

在样式表中,调用操作的元素必须具有属性 cursor:pointer; 。可能Apple会在这些调用中提出这一要求,以获得有关用户界面的最佳反馈。

At the stylesheet, the element which calls the action must have the property cursor: pointer;. Probably Apple put this requirement in these calls for best feedback on user interface.

再次在样式表中,我们无法设置显示:none; 用于隐藏输入,因为某些浏览器不接受未显示元素的点击。

Again at the stylesheet, we can't setting display: none; for hidden input because some browsers don't accept clicks on elements that aren't displayed.

链接到JSFiddle上的固定示例

这篇关于iOS 9上的Safari不会触发隐藏输入文件上的单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆