<!DOCTYPE html> <html> <head> <meta content="charset=utf-8" /> <!--设置图片上传后预览的div样式--> <style type="text/css"> *{ float:left; margin:0; } body>div{ width:200px; height:225px; text-align:center; } #preview, .img, img { width:200px; height:200px; } #preview { border:1px solid #000; } input{ width:200px; } </style> </head> <body> <input type="file" onchange="preview(this)" /> <div> <h3>图片预览</h3> <div id="preview"></div> </div> <script type="text/javascript"> function preview(file) { var prevDiv = document.getElementById(‘preview‘); if (file.files!=null && file.files[0]!=null){ 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>
时间: 2024-12-22 08:27:16