<!DOCTYPE html>
<html>
<head>
<title>美化原生HTML上传文件</title>
<style>
.input-file {
display: none;
}
.custom-file-upload {
position: relative;
display: inline-block;
overflow: hidden;
padding: 8px 12px;
border: 1px solid #ccc;
border-radius: 4px;
cursor: pointer;
}
.custom-file-upload label {
display: inline-block;
background-color: #3498db;
color: white;
padding: 6px 12px;
border-radius: 4px;
cursor: pointer;
}
.custom-file-upload label:hover,
.custom-file-upload label:active {
background-color: #2980b9;
}
#file-name {
display: inline-block;
margin-left: 10px;
}
</style>
</head>
<body>
<div class="custom-file-upload">
<input type="file" id="file-input" class="input-file">
<label for="file-input"><i class="fa fa-cloud-upload"></i>拿来吧你</label>
<span id="file-name">没有选择文件</span>
</div>
<script>
var fileInput = document.getElementById('file-input');
var fileNameLabel = document.getElementById('file-name');
fileInput.addEventListener('change', function() {
if (this.files.length > 0) {
fileNameLabel.textContent = this.files[0].name;
} else {
fileNameLabel.textContent = '没有选择文件';
}
});
</script>
</body>
</html>