input[tyle="file"]样式修改及上传文件名显示
生活随笔
收集整理的這篇文章主要介紹了
input[tyle="file"]样式修改及上传文件名显示
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
默認(rèn)的上傳樣式我們總覺得不太好看,根據(jù)需求總想改成和上下結(jié)構(gòu)統(tǒng)一的風(fēng)格……
實(shí)現(xiàn)方法和思路: 1.在input元素外加a超鏈接標(biāo)簽 2.給a標(biāo)簽設(shè)置按鈕樣式 3.設(shè)置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>
效果:
總結(jié)
以上是生活随笔為你收集整理的input[tyle="file"]样式修改及上传文件名显示的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WIN7X64SP1极限精简版by双心
- 下一篇: 使用MMS(MongoDB Monito