首先,我们的 HTML 这样写:

index.html

1
2
3
4
<a class="btn btn-primary btn-upload">
    <span>上传</span>
    <input type="file">
</a>

备注:btn 和 btn-primary 是 Bootstrap 的样式。

style.css

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
.btn-upload {
    position: relative;
    overflow: hidden;
}
 
.btn-upload input {
    position: absolute;
    top: 0;
    right: 0;
    margin: 0;
    padding: 0;
    font-size: 20px;
    cursor: pointer;
    opacity: 0;
    filter: alpha(opacity=0);
}