input[tyle="file"]樣式修改及上傳文件名顯示


默認的上傳樣式我們總覺得不太好看,根據需求總想改成和上下結構統一的風格……

實現方法和思路:

  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>

 

 效果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM