跨浏览器样式“选择文件”;按钮
Cross browser styling "choose file" button
这是我的代码:
html:<form>
<input id = "file" type="file" />
<div id="custom_button">custom button</div>
</form>
Jquery: $("#custom_button").on("click", function () {
$("#file").click();
});
css: #file {
display: none;
}
但这只适用于firefox和chrome,在safari和opera中,在点击custom button
时,文件选择窗口没有调用
演示:http://jsfiddle.net/J4GdN/
问题:为什么这在safari和opera中不起作用?在这些浏览器中有什么替代方案?
一些用户代理不允许通过js触发点击输入文件元素,特别是在css display:none中。另一种方法是:
HTML<input id="file-element" type="file" name="my-file-upload" />
<div id="file-element-replacement">
<input type="text" value="" />
<img alt="custom upload" src="custom-upload.png" />
</div>
CSS #file-element {
/* if opacity is 0 => some UAs interpret it like display:none */
filter: alpha(opacity=1);
filter: progid:DXImageTransform.Microsoft.Alpha(opacity=1);
-moz-opacity: 0.01;
-webkit-opacity: 0.01;
opacity: 0.01;
position: relative;
z-index: 2;
}
#file-element-replacement {
position: relative;
top: -20px;
z-index: 1;
}
将input-file设置为透明,并在后面添加input-text + image作为图层进行模拟。
另一个选择是使用标准的<label>
标记,将for
属性设置为<input>
的id
。
你可以根据需要隐藏<input>
和<label>
的样式。
在我的测试中,这在不同浏览器中都能很好地工作,因为它是标准功能。
相关文章:
- 选择文件后显示图像
- 单选按钮的验证(启用)&使用Jquery Optimization从复选框中选择submit按钮
- 当选择文件时更改标签内容jQuery
- 从GWT中的HighCharts库(以及一般的Javascript)自定义Stockchart范围选择器按钮
- 触发器<输入类型=“;文件“>用angular程序选择文件
- 打开'选择文件'窗口单击锚链接(跳过单击'选择文件'按钮)
- 隐藏错误消息“;未选择文件”;asp:fileUpload的文本
- 如何从 Chrome 中的文件输入中删除“未选择文件”工具提示
- 选择文件(表单提交)后无法单击“取消”按钮
- 选择文件后的 Javascript 代码
- 单击输入框以显示“打开文件”对话框,但不单击“选择文件”按钮
- 如何禁用按钮选择文件上传完成后在php中上传文件
- 打开"选择文件"对话框,点击凸起按钮材质界面
- 当用户选择文件时,更改文件输入按钮's文本
- 跨浏览器样式“选择文件”;按钮
- 更改“选择文件”;按钮,并继续显示文件名
- 当没有选择文件时,提交按钮不起作用
- 当在FileUpload组件中选择文件时,启用页面上的按钮
- 仅当通过复选框选择文件时,Django 模板中的显示按钮
- 使用JavaScript获取HTML文件选择器按钮选择的文件的绝对路径