2016-11-30 82 views

回答

0

而是改變輸入元素的文本和默認參數嘗試使用一種變通方法。

<div class="file"> 
    <label for="file-input">Pick a file</label> 
    <input type="file" id="file-input"> 
</div> 

,並通過風格的設計吧:

.file { position: relative; height: 30px; width: 100px; } 
.file > input[type="file"] { position: absoulte; opacity: 0; top: 0; left: 0; right: 0; bottom: 0 } 
.file > label { position: absolute; top: 0; right: 0; left: 0; bottom: 0; background-color: #666; color: #fff; line-height: 30px; text-align: center; cursor: pointer; } 

Working Demo

+0

感謝您的回覆。你的代碼改變了按鈕的文本,但它完全覆蓋了它的默認行爲。我無法看到我選擇的文件。 –

1

您創建一個div用一個按鈕和一個跨度:

<div> 
<input type="button" onclick="document.getElementById('fu').click()" 
value="Click to select file" /> 
<span id="fileName"></span> 
</div> 

而且把可見的div只是您的文件上傳名爲'fu'

<input type="file" id="fu" onchange="FileSelected()" style="width: 0;"> 

,並添加這個腳本:

function FileSelected(e) 
{ 
    file = document.getElementById('fu').files[document.getElementById('fu').files.length - 1]; 
    document.getElementById('fileName').innerHtml= file.name; 
} 

而且你喜歡file.type和file.size另一個有用的東西來展示。

0

您可能會增加.file > label { width: 113px; }width,並將opacity: 0;放入.file > input[type="file"]