在網站上點「瀏覽」就會跳出選擇檔案的對話框,這個功能靠的是 input 的 file 控件,寫法非常簡單:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<input type="file" value="選擇檔案" />
</body>
</html>
的成果如下:

注意了,這不是「一個文字框加一顆按鈕」拼出來的,它本身就是一個完整的 file 控件,瀏覽器負責畫出外觀,而這也是它難客製的原因。
工作上有個需求是不顯示那串提示文字,折騰了一小時才發現,把 width 調窄就能解決:
<input type="file" value="選擇檔案" />
寬度設為 70px 剛剛好:

既然原生控件的外觀動不了,就換個策略:自己畫一組假的介面,再讓真正的 file 控件透明地蓋在上面接收點擊。
外層 div 的作用是提供定位參考——因為要相對定位,讓真正的 file 控件疊在模擬介面上方,然後把 file 控件隱藏起來(讓它不可見但仍可互動)。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.file-box{ position: relative; width: 340px; }
.txt{ height: 22px; border: 1px solid #cdcdcd; width: 180px; }
.btn{ background-color: #FFF; border: 1px solid #CDCDCD; height: 24px; width: 70px; }
.file{ position: absolute; top: 0; right: 80px; height: 24px; opacity: 0; width: 260px; }
</style>
</head>
<body>
<div class="file-box">
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="textfield" id="textfield" class="txt" />
<input type="button" class="btn" value="瀏覽" />
<input type="file" name="fileField" class="file" id="fileField" size="28" />
</form>
</div>
</body>
</html>
做出來的效果:

幾個關鍵細節:opacity: 0 讓原生控件隱形但保留互動能力;right 定位確保它蓋在「瀏覽」鈕上方;表單別忘了 enctype="multipart/form-data",否則檔案上傳會失敗。