vue.js隐藏input file的方法:1、将input的z-index设置为1以上的数字并覆盖到需要点击的内容上;2、将input的样式opacity设置为0(即为透明度为0);3、通过绑定在input上的change事件触发即可。

本文操作环境:windows10系统、vue.js 2.9、thinkpad t480电脑。
vue隐藏input file一般有三种方式,一种是使用HTML的lable机制触发input事件,一种是使用使用input透明覆盖,还有一种是使用vue的ref参数直接操作input的点击事件触发来实现。那么我们该如何使用这三种方式来实现隐藏input file呢?下面我们就来一起看看这三种方法。
1、使用HTML的lable机制触发input事件
lable上的for属性绑定input的id,即可通过触发lable上的点击事件触发input的change事件
<el-link type="primary">
<label for="recordExcel">上传文件|</label>
</el-link>
<form id="recordExcelForm" style="display:none">
<input type="file" id="recordExcel" name="recordExcel" @change="fileChange" />
</form>fileChange(){};//2、使用使用input透明覆盖
将input的z-index设置为1以上的数字并覆盖到需点击的内容上,将input的样式opacity设置为0(即为透明度为0),这样通过绑定在input上的change事件触发
立即学习“前端免费学习笔记(深入)”;
<p class="uploadImg"> <input type="file" @change="picUpload($event)" accept="image/*" /></p>
.uploadImg {
width: 100%;
height: 1.46rem;
position: relative;
input {
width: 1.46rem;
height: 100%;
z-index: 1;
opacity: 0;
position: absolute;
cursor: pointer;
}
}3、使用vue的ref参数直接操作input的点击事件触发
<div class="upload-btn-box">
<Button @click="choiceImg" icon="ios-cloud-upload-outline" type="primary">点击上传</Button>
<input ref="filElem" type="file" class="upload-file" @change="getFile" style="display:none">
</div>
choiceImg(){ this.$refs.filElem.dispatchEvent(new MouseEvent('click')) }
getFile(){ console.log("成功"); }推荐学习:php培训
以上就是vue.js怎么隐藏input file的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号