将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;
}
}
2、使用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">
</div>
choiceImg(){this.$refs.filElem.dispatchEvent(new MouseEvent(\'click\'))
},
getFile(){
console.log("成功");
}
3、使用HTML的lable机制触发input事件
<label for="upfile" class="pTitleRight" @click="IDRecognition"><span>身份证识别</span>
<i class="iconfont"></i>
<input ref="filElem" type="file" accept="image/*" id="upfile" name="upfile" style="display: none;" @change="uploadPic">
</label>
IDRecognition: function() {}, //触发事件uploadPic: function() {
console.log(\'dsa\');
}
lable上的for属性绑定input的id,即可通过触发lable上的点击事件触发input的change事件 ----推荐
还没有评论,来说两句吧...