在 JavaScript 中,我可以“点击"吗?以编程方式为文件输入元素触发事件? [英] In JavaScript can I make a "click" event fire programmatically for a file input element?

查看:24
本文介绍了在 JavaScript 中,我可以“点击"吗?以编程方式为文件输入元素触发事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式在 <input type="file"> 标签上触发点击事件.

I'd like to make a click event fire on an <input type="file"> tag programmatically.

只是调用 click() 似乎没有任何作用,或者至少它没有弹出文件选择对话框.

Just calling click() doesn't seem to do anything or at least it doesn't pop up a file selection dialog.

我一直在尝试使用侦听器捕获事件并重定向事件,但我无法像点击它那样实际执行事件.

I've been experimenting with capturing events using listeners and redirecting the event, but I haven't been able to get that to actually perform the event like someone clicked on it.

推荐答案

您不能在所有浏览器中都这样做,据说 IE 确实允许这样做,但 Mozilla 和 Opera 不允许.

You cannot do that in all browsers, supposedly IE does allow it, but Mozilla and Opera do not.

当您在 GMail 中撰写邮件时,附加文件"功能在 IE 和任何支持此功能的浏览器中以一种方式实现,然后在 Firefox 和那些不支持的浏览器中以另一种方式实现.

When you compose a message in GMail, the 'attach files' feature is implemented one way for IE and any browser that supports this, and then implemented another way for Firefox and those browsers that do not.

我不知道你为什么不能这样做,但是有一件存在安全风险的事情,而且你在任何浏览器中都不允许这样做,就是以编程方式将文件名设置在HTML 文件元素.

I don't know why you cannot do it, but one thing that is a security risk, and which you are not allowed to do in any browser, is programmatically set the file name on the HTML File element.

这篇关于在 JavaScript 中,我可以“点击"吗?以编程方式为文件输入元素触发事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆