zoukankan      html  css  js  c++  java
  • Vue触发隐藏input file的方法

    1、使用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;
        }
    }

    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">&#xe612;</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事件    ----推荐

  • 相关阅读:
    shell 环境变量
    websphere 进程
    shell 安装使用VIM
    shell seq 用法
    shell 变量自增
    WebService学习笔记系列(二)
    WebService学习笔记系列(一)
    类加载器及其委托机制的深入分析
    Java工具类:给程序增加版权信息
    QQ互联API接口失效,第三方网站的死穴
  • 原文地址:https://www.cnblogs.com/wangjishu/p/11350999.html
Copyright © 2011-2022 走看看