![upload.png input[type=file]去掉“未选择任何文件”及样式改进 经验总结 第1张 input[type=file]去掉“未选择任何文件”及样式改进 经验总结 第1张](https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
input 标签的 file 属性,在没有选择文件的情况下,默认显示一条文字“未选择任何文件”。
![1.png input[type=file]去掉“未选择任何文件”及样式改进 经验总结 第2张 input[type=file]去掉“未选择任何文件”及样式改进 经验总结 第2张](https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
在选择文件后,该条文字会变成路径和文件的名称。
![2.png input[type=file]去掉“未选择任何文件”及样式改进 经验总结 第3张 input[type=file]去掉“未选择任何文件”及样式改进 经验总结 第3张](https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
如何去掉其默认的“未选择任何文件”提示呢?
查阅资料,没有发现可以直接修改的办法,但是我们可以通过其他方法,实现自定义提示文字的效果。
我通过 label 标签绑定 input 标签,然后隐藏 input ,添加 button 和 span 来自定义提示语:
HTML部分
<label for="fileinp"> <input type="button" id="btn" value="点我上传"><span id="text">请上传Word文档</span> <input type="file" id="fileinp"> </label>
CSS部分:
label{
position: relative;
}
#fileinp{
position: absolute;
left: 0;
top: 0;
opacity: 0;
}
#btn{
margin-right: 5px;
}
#text{
color: red;
}
效果如下:
![3.png input[type=file]去掉“未选择任何文件”及样式改进 经验总结 第4张 input[type=file]去掉“未选择任何文件”及样式改进 经验总结 第4张](https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
如果要选择文件后 提示语也跟着改变,可以用 jQuery 修改 span 标签的内容。
input 发生变化时,span 的内容修改为 input 的 value 值,代码如下:
$("#fileinp").change(function () {
$("#text").html($("#fileinp").val());
})
效果如图:
![4.png input[type=file]去掉“未选择任何文件”及样式改进 经验总结 第5张 input[type=file]去掉“未选择任何文件”及样式改进 经验总结 第5张](https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
如果感觉按钮比较丑,可以用CSS稍微调整一下:
#btn{
padding: 5px 10px;
background: #00b0f0;
color: #FFF;
border: none;
border-radius: 5px;
}
如下图:
![5.png input[type=file]去掉“未选择任何文件”及样式改进 经验总结 第6张 input[type=file]去掉“未选择任何文件”及样式改进 经验总结 第6张](https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
未经允许不得转载:w3h5 » input[type=file]去掉“未选择任何文件”及样式改进
原创文章,作者:3628473679,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/150148.html