function getFileName()
{
var x = document.getElementById('upload-input')
x.style.visibility = 'collapse'
document.getElementById('fileName').innerHTML = x.value.split('\\').pop()
}
<input
name="upload"
id="upload-input"
placeholder="Select a file"
ref="fileInput"
type="file"
required
onchange="getFileName()"><span id="fileName"></span>
::-webkit-file-upload-button,
::file-selector-button {
display: none;
}
::-webkit-file-upload-button,
::file-selector-button {
display: none;
}
<input type="file">
input[type="file"] {
width: 280px;
line-height: normal;
height: 32px;
/* display: none; */
}
input::-webkit-file-upload-button,
input::file-selector-button {
width:0px;
overflow:hidden;
content:"";
padding: 0;
font-size: 0;
border:none;
height: 40px;
box-shadow: none;
align-items: center;
align-self: center;
align-content: center;
}
<input type="file"">
<span id='upload-file-name'></span>
使用JavaScript,首先获取文件名
var em = document.getElementById("upload-input");
var fname = em.name;
em.style.display = 'none';
将文件名放入这个新组件中
document.getElementById("upload-file-name").innerHTML = fname;
你可以在CSS中使用颜色属性和透明度来隐藏文件名的字母,但按钮不知道。
.inputfile{
opacity: 0;
}
<input
name="upload"
id="upload-input"
placeholder="Select a file"
ref="fileInput"
type="file"
class="inputfile"
required>