Vue隐藏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(
"成功"); }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM