使用 jquery 打开文件浏览器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3767408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
open file browser using jquery
提问by Albert
I have the following code:
我有以下代码:
<p onclick="jQuery('#file').click()">Select a file</p>
<input type="file" id="file" name="file" />
When the user clicks on "Select a file", it's also supposed to open the file browser like when you press on the "Browse" button generated by <input type="file" />
, where the user can select a file.
当用户点击“选择文件”时,它也应该像按下由 生成的“浏览”按钮一样打开文件浏览器<input type="file" />
,用户可以在其中选择一个文件。
It works fine in Chrome and IE7. How do I make it work in Firefox?
它在 Chrome 和 IE7 中运行良好。我如何使它在 Firefox 中工作?
回答by Reigel
Trust me, I went to trouble already about that. It will not work on Firefox and I did not find an answer as to how to make it work.
相信我,我已经为此遇到了麻烦。它在 Firefox 上不起作用,我没有找到有关如何使其工作的答案。
I suspected you are trying to customize the looks of your input file. I suggest, you use opacity. Set opacity of input file to zero, to make it not visible. Then put above it two span element above it. Then you could style the span as much as you like. When you click any of the span, the click on input file is also trigger.
我怀疑您正在尝试自定义输入文件的外观。我建议,你使用不透明度。将输入文件的不透明度设置为零,使其不可见。然后在其上方放置两个跨度元素。然后,您可以随心所欲地设计跨度。当您单击任何跨度时,也会触发对输入文件的单击。
simple demo
简单的演示
added notes:
补充说明:
With that you have the power to change the look of input file.The problem is, opacity won't work on IE6. ;)
有了它,您就可以更改输入文件的外观。问题是,不透明度在IE6 上不起作用。;)
回答by Jason
There is a better way to open the dialog (it works across all modern browsers at this time). Just change
有一种更好的方法来打开对话框(目前它适用于所有现代浏览器)。只是改变
jQuery('#file').click();
to
到
jQuery('#file').trigger('click');