input[tyle="file"]样式修改及上传文件名显示
2016-11-01 17:50
232 查看
默认的上传样式我们总觉得不太好看,根据需求总想改成和上下结构统一的风格……
html代码:
css代码:
效果:
此时并不能显示上传的文件名,如需显示出上传的文件名需要js来处理
js代码:
同时需要给html加上两个div
效果:
实现方法和思路: 1.在input元素外加a超链接标签 2.给a标签设置按钮样式 3.设置input[type='file']为透明,并定位,覆盖在a上面
html代码:
<a class="input-file input-fileup" href="javascript:;"> + 选择文件<input size="100" type="file" name="file" id="file"> </a>
css代码:
.input-file{ display: inline-block; position: relative; overflow: hidden; text-align: center; width: auto; background-color: #2c7; border: solid 1px #ddd; border-radius: 4px; padding: 5px 10px; font-size: 12px; font-weight: normal; line-height: 18px; color:#fff; text-decoration: none; } .input-file input[type="file"] { position: absolute; top: 0; right: 0; font-size: 14px; background-color: #fff; transform: translate(-300px, 0px) scale(4); height: 40px; opacity: 0; filter: alpha(opacity=0); }
效果:
此时并不能显示上传的文件名,如需显示出上传的文件名需要js来处理
js代码:
<script> $(function(){ $(".input-fileup").on("change","input[type='file']",function(){ var filePath=$(this).val(); if(filePath.indexOf("jpg")!=-1 || filePath.indexOf("png")!=-1){ $(".fileerrorTip1").html("").hide(); var arr=filePath.split('\\'); var fileName=arr[arr.length-1]; $(".showFileName1").html(fileName); }else{ $(".showFileName1").html(""); $(".fileerrorTip1").html("您未上传文件,或者您上传文件类型有误!").show(); return false } }) }) </script>
同时需要给html加上两个div
<a class="input-file input-fileup" href="javascript:;"> + 选择文件<input size="100" type="file" name="file" id="file"> </a>
<div class="fileerrorTip1"></div>
<div class="showFileName1"></div>
效果:
相关文章推荐
- input(file)样式修改及上传文件名显示
- 上传文件控制input type="file"样式修改记
- 修改input[type="file"]文件上传样式的2种方法
- 上传文件 隐藏input type="file",用text显示
- html中,文件上传时使用的<input type="file">的样式自定义
- ajaxfileupload单文件上传兼容IE8及 input type=file样式修改
- <input type="file">定义样式并获取上传文件路径及指定文件类型
- 小技巧实现修改input-file样式,代理上传
- 如何修改input[type="file"]的默认样式
- 更改上传文件的样式<input type="file>
- 修改 上传图片按钮input-file样式。。
- input type="file"在各个浏览器下的默认样式,以及修改自定义样式
- 上传按钮样式优化 <input type="file" />
- input[type="file"]的样式修改
- input,type="file"的样式修改
- <input type="file">更改 页面显示 浏览 为上传 .
- 上传文件input tyle="file"文件的浏览按钮和文本框用图片替换的方法1
- 修改input file文件上传的默认样式 兼容ie8
- <input type="file">定义样式并获取上传文件路径及指定文件类型
- 修改 input[type="file"] 原生样式