input上传图片

Posted 王子乔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了input上传图片相关的知识,希望对你有一定的参考价值。

1.通过input自身的onchange事件触发:

<input id="file" type="file" accept="image/*" onchange=‘changeFile()‘>
changeFile: function () {
  let myFile = document.getElementById(‘file‘).files[0] // 获取图片内容
}

2.通过事件绑定触发:

<input id="file" type="file" accept="image/*">
changeFile: function () {
  let file = document.getElementById(‘file‘)
  file.addEventListener(‘change‘, function () {
    let myFile = file.files[0] // 获取图片内容
  })
}
changeFile()
 

 

 

 

以上是关于input上传图片的主要内容,如果未能解决你的问题,请参考以下文章