上传图片预览
Posted 北执
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了上传图片预览相关的知识,希望对你有一定的参考价值。
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <style type="text/css"> #preview, .img, img { width:200px; height:200px; } #preview { border:1px solid #dedede; } /*a upload */ .a-upload { width:200px; height: 200px; text-align:center; text-decoration: none; line-height: 20px; position: relative; bottom:202px; opacity:0; cursor: pointer; color: #888; background: #fafafa; border: 1px solid #ddd; border-radius: 4px; overflow: hidden; display: inline-block; *display: inline; *zoom: 1 } .a-upload input { position: absolute; font-size: 16px; right: 0; top: 0; opacity: 0; filter: alpha(opacity=0); cursor: pointer text-decoration:none; width:200px; height: 200px; } .a-upload:hover { } </style> </head> <body> <div id="preview"></div> <a href="javascript:;" class="a-upload"> <input type="file" onchange="preview(this)" /> </a> <script type="text/javascript"> function preview(file) { var prevDiv = document.getElementById(‘preview‘); if (file.files && file.files[0]) { var reader = new FileReader(); reader.onload = function(evt){ prevDiv.innerHTML = ‘<img src="‘ + evt.target.result + ‘" />‘; } reader.readAsDataURL(file.files[0]); } else { prevDiv.innerHTML = ‘<div class="img" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src=\‘‘ + file.value + ‘\‘"></div>‘; } } </script> </body> </html>
以上是关于上传图片预览的主要内容,如果未能解决你的问题,请参考以下文章