简体   繁体   English

Javascript专注于文件输入的浏览按钮

[英]Javascript focus on browse button of file input

I am trying to focus on the browse button of the file input control. 我正在尝试着重于文件输入控件的浏览按钮。

so I have something like 所以我有类似

 <input type="file" name="upload" id="upload" >

and in javascript I have 在JavaScript中

document.getElementById("upload").focus();

but the focus remain on the text field and comes to browse button only after i hit tab. 但是焦点仍然停留在文本字段上,并且只有在我按下Tab键之后才进入浏览按钮。 Is there a way that I could write a script to set the focus on the browse button? 有没有一种方法可以编写脚本来将焦点设置在浏览按钮上?

Thanks for your help! 谢谢你的帮助!

Can't be done. 不能做 You have almost zero control over the constituent parts of a file upload field, partly for security reasons and partly because the standards do not define what constituent parts a file upload field might have. 您几乎对文件上载字段的组成部分几乎没有任何控制,部分是出于安全原因,部分是因为标准没有定义文件上载字段可能具有的组成部分。 It is entirely possible the browser might render a file upload interface without any 'Browse' button. 浏览器完全有可能在没有任何“浏览”按钮的情况下呈现文件上传界面。

This question's been around for a while, allowing standards to evolve and develop with new functionality, but I've had success with the following code: 这个问题已经存在了一段时间,它允许标准随着新功能的发展而发展,但是我在以下代码方面取得了成功:

<input type="file" id="file_field" />

<input type="button" value="click me"
    onclick="document.getElementById('file_field').click()" />

I've managed to test it in Chrome and Internet Explorer 7, 8 and 9 so far, and I suspect it would work in Firefox too. 到目前为止,我已经设法在Chrome和Internet Explorer 7、8和9中对其进行了测试,我怀疑它也可以在Firefox中使用。 Hope this helps someone! 希望这对某人有帮助!

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

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