input file樣式,文件路徑、文件名的獲取

http://blog.csdn.net/comikey/article/details/8954479
 
解決思路是把input 放在文字的上邊,弄成透明的,這樣在點文字時,實際是點擊了input,這樣就實現了文件的上傳。是否是很簡單呀。
 
具體代碼:
 
<style>
#uploadImg{ font-size:12px; overflow:hidden; position:absolute}
#file{ position:absolute; z-index:100; margin-left:-180px; font-size:60px;opacity:0;filter:alpha(opacity=0); margin-top:-5px;}
</style>  
<span id="uploadImg">
<input type="file" id="file" size="1" >
<a href="#">上傳圖片</a> </span>
 
這樣你就能夠對樣式進行想要的改變,改爲圖片,仍是文字帶背景。。。等等 想怎麼改就怎麼改。
並且還兼容ie6/ie7/firefox
 
1 <li class="f_input"><span class="t">上傳簡歷:</span>
2    <span id="uploadImg">
3       <input type="file" id="file" size="1" >
4         <a href="#">點擊上傳簡歷</a> 
5    </span>
6    <p id="em">未上傳文件</p>
7 </li>
1 .sq_list li.f_input #em{margin-left: 195px;line-height: 32px;color: #666;font-size: 13px;}
2 #uploadImg{cursor:pointer; overflow:hidden; position:relative;width: 104px;height: 32px;}
3 #file{ cursor:pointer;position:absolute; z-index:100; left:0;top:0;width: 104px;height: 32px;opacity:0;filter:alpha(opacity=0);}
4 #uploadImg a{cursor:pointer;background:#0e2d43;position: absolute;top:0;left:0;display: block;width: 104px;height: 32px;text-align: center;line-height: 32px;color:white;font-size:14px;font-weight:normal;}

文件名的傳遞 ---全路徑獲取數組

$('#file').change(function(){
    $('#em').text($('#file').val());
});

文件名的傳遞 ---只獲取文件名spa

1 var file = $('#file'),
2     aim = $('#em');
3     file.on('change', function( e ){
4         //e.currentTarget.files 是一個數組,若是支持多個文件,則須要遍歷
5         var name = e.currentTarget.files[0].name;
6         aim.text( name );
7     });
相關文章
相關標籤/搜索