如何使用javascript打开文件/浏览对话框? [英] How to open a file / browse dialog using javascript?
本文介绍了如何使用javascript打开文件/浏览对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当使用javascript点击< a href>
链接时,有没有办法打开浏览文件对话框?它应该像普通的文件浏览按钮一样工作,并给出响应中选择的文件的名称/列表。
Is there any way to open the browse for files dialog box when a <a href>
link is clicked using javascript? It should function like a normal browse for files button and give the names/list of files selected in response.
推荐答案
这是一个非jQuery解决方案。请注意,您不能只使用 .click()
,因为有些浏览器不支持它。
Here is a non-jQuery solution. Note you can't just use .click()
as some browsers do not support it.
<script type="text/javascript">
function performClick(elemId) {
var elem = document.getElementById(elemId);
if(elem && document.createEvent) {
var evt = document.createEvent("MouseEvents");
evt.initEvent("click", true, false);
elem.dispatchEvent(evt);
}
}
</script>
<a href="#" onclick="performClick('theFile');">Open file dialog</a>
<input type="file" id="theFile" />
这篇关于如何使用javascript打开文件/浏览对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文