图片选择预览(使用URL.createObjectURL)

我们在开发过程中经常会有上传图片并预览的需求

<h2>
   <input type="file" id=“importImg”>
   <img src="" id="showImg" >
</h2>

选择文件之后,根据文件来生成地址。这个url能够被对应的标签识别,把这个url的值赋给预览的img的src即可显示

$("#importImg").on("change",function(){
    // 生成url
   $("#showImg").attr("src",URL.createObjectURL($(this)[0].files[0]));
});

猜你喜欢

转载自blog.csdn.net/qidasheng2012/article/details/84637486