vue.js隱藏input file的方法:1、將input的z-index設(shè)置為1以上的數(shù)字并覆蓋到需要點(diǎn)擊的內(nèi)容上;2、將input的樣式opacity設(shè)置為0(即為透明度為0);3、通過綁定在input上的change事件觸發(fā)即可。
本文操作環(huán)境:windows10系統(tǒng)、vue.js 2.9、thinkpad t480電腦。
vue隱藏input file一般有三種方式,一種是使用HTML的lable機(jī)制觸發(fā)input事件,一種是使用使用input透明覆蓋,還有一種是使用vue的ref參數(shù)直接操作input的點(diǎn)擊事件觸發(fā)來實(shí)現(xiàn)。那么我們該如何使用這三種方式來實(shí)現(xiàn)隱藏input file呢?下面我們就來一起看看這三種方法。
1、使用HTML的lable機(jī)制觸發(fā)input事件
lable上的for屬性綁定input的id,即可通過觸發(fā)lable上的點(diǎn)擊事件觸發(fā)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設(shè)置為1以上的數(shù)字并覆蓋到需點(diǎn)擊的內(nèi)容上,將input的樣式opacity設(shè)置為0(即為透明度為0),這樣通過綁定在input上的change事件觸發(fā)
<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參數(shù)直接操作input的點(diǎn)擊事件觸發(fā)
<div class="upload-btn-box"> <Button @click="choiceImg" icon="ios-cloud-upload-outline" type="primary">點(diǎn)擊上傳</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("成功"); }
推薦學(xué)習(xí):php培訓(xùn)