html - 为什么这个自定义输入文件在 Edge 中不起作用?
问题描述
见鬼,我正在构建一个自定义输入文件,它适用于 Chrome、FF、Safari,但不适用于 Edge,知道吗?
这是我的演示。请在 Chrome 上打开它,然后在 Edge 上打开它以了解问题:
/* /////////////////Custom Upload input///////////// */
.custom-file-input::-webkit-file-upload-button {
visibility: hidden;
-webkit-appearance: none;
}
.custom-file-input::-ms-file-upload-button {
visibility: hidden;
-ms-appearance: none;
}
.custom-file-input::before {
content: 'Attach';
display: inline-block;
background-color: #c6c6c6;
border: none;
width: 50px;
font-family: Roboto, sans-serif, FontAwesome;
border-radius: 3px;
padding: 5px 8px;
outline: none;
white-space: nowrap;
-webkit-user-select: none;
font-weight: normal;
font-style: normal;
font-stretch: normal;
cursor: pointer;
color: #ffffff;
font-size: 12px;
text-align: center;
}
.custom-file-input:hover::before {
border-color: black;
}
.custom-file-input:active::before {
background: -webkit-linear-gradient(top, #e3e3e3, #f9f9f9);
}
<input class="custom-file-input" type="file">
解决方案
您应该使用::-ms-browse
Edge 中的文件输入按钮样式。然后我们必须用标签包装输入类型文件并在 CSS 中做一些事情。而如果想改变Edge中默认的文件输入框,需要先用隐藏input[type="file"] { opacity: 0;}
,结合js代码显示文件名。您可以在下面的 Edge 和 Chrome 中查看我的样式输入类型文件示例:
$('input[type=file]').change(function(e) {
$in = $(this);
$in.next().html($in.val());
});
.custom-file-input::-webkit-file-upload-button {
visibility: hidden;
-webkit-appearance: none;
}
::-ms-browse {
display:none;
}
input[type="file"] {
display: input-block;
width: 100%;
height: 30px;
opacity: 0;
cursor:pointer;
position:absolute;
left:0;
}
.custom-file-input::before {
content: 'Attach';
display: inline-block;
background-color: #c6c6c6;
border: none;
width: 50px;
font-family: Roboto, sans-serif, FontAwesome;
border-radius: 3px;
padding: 5px 8px;
outline: none;
white-space: nowrap;
-webkit-user-select: none;
font-weight: normal;
font-style: normal;
font-stretch: normal;
cursor: pointer;
color: #ffffff;
font-size: 12px;
text-align: center;
}
.custom-file-input:hover::before {
border-color: black;
}
.custom-file-input:active::before {
background: linear-gradient(top, #e3e3e3, #f9f9f9);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label class="custom-file-input">
<input type="file">
<span class="fileName">Choose file...</span>
</label>
推荐阅读
- javascript - 前端Node JS的基本理解
- python - 将包安装到 PyCharm 时遇到问题
- html - 为什么前五个切片顺时针不悬停,而其余切片则悬停,有没有办法在每个“切片”的左侧和右侧放置边框?
- formula - 从 Google Sheet 到 WPS 电子表格的过滤功能
- nginx - CORS 已配置但无响应
- android - 无法在我的手机中运行颤振的移动应用程序
- git - 如何在保留合并的同时使用 on 命令对分支进行 git rebase?
- python - 将几列中的列表组合成一个嵌套列表 pandas
- email - 为自由职业者成功发送真正寻求的个人电子邮件(不是垃圾邮件)
- r - 更改一组堆叠条形图中的轴标签