chrome 66下setTimeout回调中触发input type=file的click事件,无法弹出文件选择框?

新手上路,请多包涵

问题描述

chrome 66,在setTimeout回调中触发type为file的input框的点击事件,文件选择框是否弹出和时长有关
操作系统:windows 7
浏览器:chrome 66
1000ms以上:上传的文件框未弹出
1000ms以下:上传的文件框弹出

操作系统:windows 7
浏览器:chrome 75
5000ms以上:上传的文件框未弹出,控制台有提示File chooser dialog can only be shown with a user activation
5000ms以下:上传的文件框弹出

相关代码

<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>

</head>
<body>

<div class="button">
    <button onclick="clickBtn()">请选择文件</button>
</div>
<div class="input" style="display: none;">
    <input type="file" id="fileInput">
</div>
<script>
    var clickBtn = function() {
        setTimeout(function() {
            console.log('input click')
            document.querySelector('#fileInput').click();
        }, 1000);
    }
</script>

</body>
</html>

疑问
1.问什么会因为时长不一样,文件选择框弹出的表现不一致
2.chrome 75,同样都是模拟用户点击,为什么会因为时长不一样,5000ms以上就会打印提示File chooser dialog can only be shown with a user activation

阅读 9.2k
2 个回答
新手上路,请多包涵
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题