html - 单击文件输入标签的解决方法 (Firefox)

标签 html firefox file-upload label

<label for="input">Label</label><input type="file" id="input"/>

在 Firefox 7 中,无法通过单击标签来触发打开文件对话框。

This SO question非常相似,但它是绿色的,它是 FF 中的一个错误。我正在寻找解决方法。

有什么想法吗?

最佳答案

感谢您的问答...帮我解决了问题。

我对@marten-wikstrom 解决方案的变体:

if($.browser.mozilla) {
  $(document).on('click', 'label', function(e) {
    if(e.currentTarget === this && e.target.nodeName !== 'INPUT') {
      $(this.control).click();
    }
  });
}

笔记

  • 在任一解决方案中都不需要使用 document.ready ( $(function() {...});)。 jQuery.fn.live在@marten-wikstrom 的案例中处理这个问题;明确绑定(bind)到 document在我的示例中。
  • 使用jQuery.fn.on ...当前推荐的绑定(bind)技术。
  • 添加了 !== 'INPUT'检查以确保执行不会陷入此处的循环:

    <label>
      <input type="file">
    </label>
    

    (因为文件字段点击会冒泡回到标签)

  • 更改 event.target查看event.currentTarget , 允许初始点击 <em>在:

    <label for="field">click <em>here</em></label>
    
  • 使用标签元素的control更清晰、更简单、基于规范的表单字段关联的属性。

关于html - 单击文件输入标签的解决方法 (Firefox),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7742278/

相关文章:

html - 研究在 CSS 中创建结合百分比和静态(例如像素)值的网格

php - 使用 MySQL 数据库中的值填充 HTML 下拉列表并使其成为 'selected',而列表中还有其他选项

javascript - 有没有办法在网页中使用 `chrome` 命名空间?

html - 使用 HTML5 将文件上传到 Google Drive API

c# - 将上传的文件作为附件发送

php - 基于php if语句添加/更改div中的文本

jquery - Bootstrap 4 Accordion 切换全部

html - 为什么在Firefox中默认禁用 "dialog"标签?

javascript - 错误: "Access to restricted URI denied"

file-upload - 如何安全传输文件