简体   繁体   English

如何在 onload 事件上单击输入(类型=文件)

[英]How to click an input (type=file) on onload event

Current / code...当前的 / 代码...

 function myfunction() { alert("hello"); document.getElementById('file').click(); }
 <body onload="myfunction();"> <input type="file" id="file" /> </body>

Now alert is displayed, but file is not clicked on onload event but if we change the event to onclick , the alert is shown as well as file is also clicked.现在显示alert ,但文件未在onload事件上单击,但如果我们将事件更改为onclick ,则显示alert以及单击文件。

Based on the security model of the modern browsers this is not possible.基于现代浏览器的安全模型,这是不可能的。 The click function will not work until the user has interacted with the page.在用户与页面交互之前, click功能将不起作用。 The best I could come up with is theonfocus event which requires the user to do very little to interact with the page.我能想到的最好的方法是onfocus事件,它要求用户很少与页面交互。

Try this...尝试这个...

 function myfunction() { alert("hello"); document.getElementById('file').click(); }
 <body onfocus="myfunction();"> <input type="file" id="file" /> </body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM