文件输入标签单击的解决方法 (Firefox)
<label for="input">Label</label><input type="file" id="input"/>
在 Firefox 7 中,无法通过单击标签来触发打开文件对话框。
这个问题非常相似,但它是绿色的,这是一个FF 中的错误。我正在寻找解决方法。
有什么想法吗?
<label for="input">Label</label><input type="file" id="input"/>
In Firefox 7 it is not possible to trigger the open file dialog by clicking on the label.
This SO question is very similar but that's green checked with it's a bug in FF. I'm looking for a workaround.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
谢谢你的问答...帮助了我。
我对 @marten-wikstrom 解决方案的变体:
在任一解决方案中,使用 document.ready (
$(function() {...});
)的注释jQuery.fn.live
负责处理 @marten-wikstrom 的情况;在我的示例中,显式绑定到document
是这样。添加了
!== 'INPUT'
检查以确保执行不会陷入循环:<前><代码><标签>
<输入类型=“文件”>
(因为文件字段单击将冒泡回到标签)
将
event.target
检查更改为event.currentTarget
,允许初始单击< ;em>
中:control
属性来实现更清晰、更简单、基于规范的表单字段关联。thank you for this q&a... helped me out.
my variation of @marten-wikstrom's solution:
notes
$(function() {...});
) is unnecessary, in either solution.jQuery.fn.live
takes care of that in @marten-wikstrom's case; explicitly binding todocument
does in my example.jQuery.fn.on
... current recommended binding technique.added the
!== 'INPUT'
check to ensure execution does not get caught in a loop here:(since the file field click will bubble back up to the label)
change
event.target
check toevent.currentTarget
, allowing for initial click on the<em>
in:control
attribute for cleaner, simpler, spec-base form field association.我想出了一个可行的解决方法:
很奇怪的是,FF 允许您模拟文件输入上的点击。我认为这被认为是安全风险...
更新:这是一个通用的解决方法:
I came up with a feasible workaround:
Quite strange that FF allows you to simulate a click on a file input. I thought that was considered a security risk...
UPDATE: This is a generic workaround:
使用 jQuery 浏览器检测时会出现一些问题,最明显的是使用浏览器检测而不是功能检测的反模式,此外 1.9+ 不提供该功能。
也许,我得出的解决方案有点虚伪,但它运作良好,并且似乎遵循了当今的大多数最佳实践。
首先,确保您使用 Paul Irish 的条件类。然后,使用类似:
否则,我发现该事件将在 Chrome 等浏览器中双重触发。这个解决方案看起来足够优雅。
A couple problems arise when using the jQuery browser detection, most notably the anti-pattern of using browser detection rather than feature detection, in addition to the fact that 1.9+ doesn't provide that functionality.
Perhaps, then, the solution I arrived at is a bit hypocritical, but it worked well and seems to adhere to most best practices today.
First, ensure you're using Paul Irish's conditional classes. Then, use something like:
Otherwise, I found the event would be double-fired in browsers such as Chrome. This solution seemed elegant enough.
文件选择对话框可以通过
click()
事件。此问题的一个不显眼的解决方案可能如下所示:删除
for
属性很重要,因为其他浏览器(例如 Chrome、IE)仍然会批准它并显示对话框两次。我在 Chrome 25、Firefox 19 和 IE 9 中测试了它,效果非常好。
The file-selection dialog can be triggered in all browsers by the
click()
event. An unobtrusive solution to this problem could look like that:Removing the
for
attribute is important since other browsers (e.g. Chrome, IE) will still ratify it and show the dialog twice.I tested it in Chrome 25, Firefox 19 and IE 9 and works like a charm.
它似乎在 FF 23 中得到了修复,因此浏览器检测变得危险并导致双系统对话框;(
您可以添加另一个测试来限制对版本 23 之前的 FF 版本的修复:
它非常丑陋,但此修复将被删除,因为一旦旧版本的FF就会消失。
It seems to be fixed in FF 23, so browser detection becomes hazardous and leads to double system dialogs ;(
You can add another test to restrict the fix to FF version prior to version 23:
It's quite ugly, but this fix will be removed as soon as old the version of FF will have disappeared.
当您不需要/不想有输入框(例如图像上传)时,解决方法是在元素中使用
opacity: 0
并使用pointer-events: none;
代码> 在标签中。该解决方案确实是特定于设计的,但也许应该适用于遇到此问题的人。 (到目前为止,该错误尚未修复)
http://codepen.io/octavioamu/pen/ByOQBE< /a>
A work around when you don't need/want to have the input box (like image upload) is to use
opacity: 0
in the element and usepointer-events: none;
in the label.The solution is really design specific but maybe should work for someone who comes to this. (until now the bug doesn't been fixed)
http://codepen.io/octavioamu/pen/ByOQBE
颠倒标签和输入元素的顺序。 iow,将标签元素放在输入元素之后。
Reverse the order of the label and input elements. iow, put the label element after the input element.
如果需要,您可以将任何事件中的事件分派到 type=file 输入
使输入显示:无和可见性:隐藏,然后从以下位置调度事件:
比如说,点击|触摸图像......
you can dispatch the event from any event to the type=file input if you want
make the input display:none and visibility:hidden, and then dispatch the event from,
say, the click|touch of an image ...
在
React
环境中使用上面 Corey 的答案,我必须执行以下操作:(Firefox 检查基于:如何检测 Safari、Chrome、IE、Firefox 和 Opera 浏览器?)
Using the answer of Corey above in a
React
environment I had to do the following:(Firefox check is based on: How to detect Safari, Chrome, IE, Firefox and Opera browser?)
试试这个代码
Try this code