隐藏input type = file上的浏览按钮

jos*_*osh 18 html css

有没有办法隐藏浏览按钮,只留下适用于所有浏览器的文本框?

我已经尝试设置边距,但它们在每个浏览器中显示不同

Ort*_*iga 20

不,你可以做的是一个(丑陋的)解决方法,但很大程度上使用

  1. 创建普通输入和图像
  2. 使用不透明度0创建文件输入
  3. 当用户单击图像时,您将模拟文件输入的单击
  4. 当文件输入改变时,将它的值传递给正常输入(因此用户可以看到路径)

在这里,您可以看到完整的解释以及代码:

http://www.quirksmode.org/dom/inputfile.html

  • 我认为浏览器将来出于安全考虑可能会禁止这样做。 (2认同)

小智 11

您可能只是在不隐藏元素的情况下,只需将其不透明度设置为0即可使其透明.

使输入文件隐藏将使其停止工作.所以不要那样做..

在这里,您可以找到透明浏览操作的示例;


小智 8

下面的代码对于隐藏默认浏览按钮和使用自定义非常有用:

(function($) {
  $('input[type="file"]').bind('change', function() {
    $("#img_text").html($('input[type="file"]').val());
  });
})(jQuery)
Run Code Online (Sandbox Code Playgroud)
.file-input-wrapper {
  height: 30px;
  margin: 2px;
  overflow: hidden;
  position: relative;
  width: 118px;
  background-color: #fff;
  cursor: pointer;
}

.file-input-wrapper>input[type="file"] {
  font-size: 40px;
  position: absolute;
  top: 0;
  right: 0;
  opacity: 0;
  cursor: pointer;
}

.file-input-wrapper>.btn-file-input {
  background-color: #494949;
  border-radius: 4px;
  color: #fff;
  display: inline-block;
  height: 34px;
  margin: 0 0 0 -1px;
  padding-left: 0;
  width: 121px;
  cursor: pointer;
}

.file-input-wrapper:hover>.btn-file-input {
  //background-color: #494949;
}

#img_text {
  float: right;
  margin-right: -80px;
  margin-top: -14px;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<body>
  <div class="file-input-wrapper">
    <button class="btn-file-input">SELECT FILES</button>
    <input type="file" name="image" id="image" value="" />
  </div>
  <span id="img_text"></span>
</body>
Run Code Online (Sandbox Code Playgroud)


Krz*_*oda 8

截至 2022 年,现代浏览器支持文件按钮伪选择器。我只是在 Safari v16.1 上苦苦挣扎,它没有按预期工作,必须解决按钮隐藏问题(::-webkit-file-upload-button部分)。

input[type=file]::file-selector-button {
    display: none;
}

input[type=file]::-webkit-file-upload-button {
    display: block;
    width: 0;
    height: 0;
    margin-left: -100%;
}

input[type=file]::-ms-browse {
    display: none;
}
Run Code Online (Sandbox Code Playgroud)

您还可以使用简洁的语法:

::file-selector-button {
    /* ... */
}

::-webkit-file-upload-button {
    /* ... */
}

::-ms-browse {
    /* ... */
}
Run Code Online (Sandbox Code Playgroud)


小智 7

遇到这个问题,感觉没有任何答案是干净的。这是我的解决方案:

<label>
  <span>Select file</span>
  <input type="file" style="display: none">
</label>
Run Code Online (Sandbox Code Playgroud)

单击标签时,将打开选择文件对话框。不需要 js 来实现它。

您可以将标签设置为看起来像按钮。

这是使用 w3css 和 font Awesome 的示例:

<label class="w3-button w3-blue w3-round">
    <span><i class="fas fa-image"></i></span>
    <input type="file" style="display: none" >
</label>
Run Code Online (Sandbox Code Playgroud)

当然,您需要向输入添加一个事件侦听器以检测是否选择了文件。


Pix*_*ech 5

        .dropZoneOverlay, .FileUpload {
            width: 283px;
            height: 71px;
        }

        .dropZoneOverlay {
            border: dotted 1px;
            font-family: cursive;
            color: #7066fb;
            position: absolute;
            top: 0px;
            text-align: center;
        }

        .FileUpload {
            opacity: 0;
            position: relative;
            z-index: 1;
        }
Run Code Online (Sandbox Code Playgroud)
        <div class="dropZoneContainer">
            <input type="file" id="drop_zone" class="FileUpload" accept=".jpg,.png,.gif" onchange="handleFileSelect(this) " />
            <div class="dropZoneOverlay">Drag and drop your image <br />or<br />Click to add</div>
        </div>
Run Code Online (Sandbox Code Playgroud)

我找到了一个很好的方法来实现这一点在input = file删除浏览按钮.

此解决方案背后的基本原理是它创建一个透明的input = file控件,并在文件控件下创建一个对用户可见的图层.input = file的z-index将高于该层.

有了这个,它似乎是文件控件本身.但实际上当你点击它时,输入=文件是单击的文件,将出现选择文件的对话框.

  • 技术 这是最好的方法。我想最简单的复杂性和最少的努力。 (2认同)