1. 程式人生 > >自定義input file樣式

自定義input file樣式

html

<div class="costs-uploadfile-div">                             
	<input type="file" name="file" id="fileField"/> 
	<input type='text' id="textfield" placeholder="請選擇檔案!" /> 
	<button class="allBtn costs-marl15">瀏覽</button>
</div> 

css

div.costs-uploadfile-div{
    position:relative;
}
div.costs-uploadfile-div #textfield{
    width:150px;
    height:30px;    
}
div.costs-uploadfile-div #fileField{
    width:245px;
    height:30px;    
    position: absolute;
    top: 0;
    left:0;
    filter: alpha(opacity:0);
    opacity: 0;
}
div.costs-uploadfile-div .allBtn{
    padding:0;
    margin:0;
    height: 30px;
    line-height: 30px;
    width: 80px;
    background-color: #18b3cf;
    border: none;
    color: #fff;
}

js

document.getElementById("fileField").onchange = function() {
	  document.getElementById("textfield").value = document.getElementById("fileField").value;
}