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