在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML5  HTML/ VUE項(xiàng)目中無法觸發(fā)隱藏input[type=file]的事件

VUE項(xiàng)目中無法觸發(fā)隱藏input[type=file]的事件

VUE的一個(gè)頁(yè)面組件在activeted的時(shí)候調(diào)用一個(gè)函數(shù),創(chuàng)建一個(gè)input[type=file]標(biāo)簽,然后點(diǎn)擊。
函數(shù)代碼如下:

quickPhoto() {
        // 創(chuàng)建input
      let file = document.createElement('input')
      file.setAttribute('type', 'file')
      file.setAttribute('capture', 'camera')
      file.setAttribute('accept', 'image/*')
      file.style.display = 'none'
      document.querySelector('body').appendChild(file)
      file.addEventListener('change', ()=>{
        this.$vux.loading.show()
        //壓縮圖片
        this.$utils.$imgCompress(file.files[0]).then(img=>{
          this.$vux.loading.hide()
          this.params.problemImgs.push(img)
          file.value=""
          delete file.parentNode.removeChild(file)
        }).catch(e=>{
          this.$vux.loading.hide()
          alert(e)
        })
      })
      file.addEventListener('click', ()=>{
        console.log('click')
      })
      file.click()
      window.quickPhoto = window.quickPhoto || this.quickPhoto
    }

頁(yè)面打開后可以在console看到click,但是沒有彈出選擇圖片的窗口。
但是在控制臺(tái)手動(dòng)調(diào)用quickPhoto()就可以彈出窗口選擇圖片,
求解。

回答
編輯回答
孤慣

我遇到過類似問題..找了很久原因,,其實(shí)是js的垃圾回收機(jī)制問題

你只要把 let file = document.createElement('input')
改為 this.file = document.createElement('input')
就不會(huì)有問題了
,
然后這句話不是必須的..可以去掉
document.querySelector('body').appendChild(file)

2017年12月8日 06:57
編輯回答
傲嬌范

瀏覽器不允許js腳本自動(dòng)打開文件選擇, 必須是用戶操作才可以

2017年4月18日 03:36