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上传图片的主要内容,如果未能解决你的问题,请参考以下文章

input file图片上传预览

Js实现input上传图片并显示缩略图

input实现多文件上传及图片预览

9行代码实现图片上传和预览(自定义按钮上传)

input file图片上传预览js实现

9行代码实现图片上传和预览(自定义按钮上传)