首页 > 开发 > JS > 正文

如何通过js实现图片预览功能【附实例代码】

2024-05-06 16:30:05
字体:
来源:转载
供稿:网友
如何通过js实现图片预览功能呢?下面小编就为大家带来一篇用js实现图片预览功能。给大家做个参考吧,一起跟随小编过来看看
 

实现代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>js图片预览功能</title><script language=javascript>function previewFile() { var preview = document.querySelector('img'); var file  = document.querySelector('input[type=file]').files[0]; var reader = new FileReader(); reader.onloadend = function () {  preview.src = reader.result; } if (file) {  reader.readAsDataURL(file); } else {  preview.src = ""; }}</script></head><body><input type="file" onchange="previewFile()"><br><img src="" height="200" width="300" src="/uploads/allimg/160331/14593014X-0.jpg" style="border: 1px solid rgb(204, 204, 204); border-image-source: none; vertical-align: bottom; padding: 1px; overflow: hidden; max-width: 650px;" />

以上这篇如何通过js实现图片预览功能【附实例代码】就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持VeVb武林网。



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表