有没有一种方法可以使用 CSS 让所有浏览器中的
<input type="file">
看起来相同?<input type="file">
看起来相同?Quirksmode.org有一篇关于此的有趣文章:http://www.quirksmode.org/dom/inputfile.html
(说明:此文提供了关于HTML文件上传控件的详细信息,建议阅读。)尝试这个方法 -
style="display:none"
,并在JavaScript中处理点击事件。更多阅读材料:使用click()方法使用隐藏的文件输入元素
编辑:出于安全原因,这种方法不再起作用。一个小技巧是将CSS样式设置为visibility:hidden
。这里有一个JSFiddle,我将我的<input>
字段放在页面末尾,并使用<a>
元素触发打开文件对话框。//html
<input id="openFileDialog" type="file" id="files" name="files[]" accept=".json, .txt" />
//js
$("#openFile").on("click", function () {
//throws an error if browser doesn't support file upload
checkFileApiCompatibility();
//opens File Dialog
$("#openFileDialog").click();
$("#openFileDialog").on('change', function(){
openFile(this);
});
});